Regex to remove text inside brackets
Yesterday’s issue of the day was to find out a nice way of removing part of a string. I was hacking together an XML sitemap for nearesthotels.com, but my seed information had some information that needed to be removed. Nearest hotels has a nice feature that you can go to any url in the format /go/country/city/ and it will display hotels on a map for you. This is done with some nice .htaccess rewriting and some flexible programming (maybe something for another day). As all combinations cities were possible, I wanted to feed google the top 1000 cities in the world to put in my sitemap. I was thinking that should be enough to get a bit of traffic for the website. My seed list was mostly well formed, except I didn’t need information contain in brackets.
The problem
I wanted to remove part of the string that was unneeded, it was sometimes in the middle of strings and surrounded by brackets. Enter in regex to the rescue to search and remove.
A typical string looked like
Xinyi (Guangdong), China San Jose (CA), United States Jacksonville (FL), United States
and I wanted it to be
Xinyi, China San Jose, United States Jacksonville, United States
My code
$re = "/\\(.*\\)/";// regex
//remove anything inside () in string $into[$row]['city']
preg_replace($re, '', ($info[$row]['city'], 1)
Uses
Code like this can be useful for removing
- prefixes in phone numbers
- comments in brackets in text paragraphs.
Some other quick points
While searching for some resources to do with Regex I came across https://regex101.com a really useful resource for text regex code.
If you’d like to generate your own XML sitemap, to feed to google or other search engines, you just need to make sure it’s in the following form.
2 Comments
Gerry Downey October 12, 2015 -
Hey Paul just stumbled across your post and that regex 101 resource you listed is a lifesaver, I have not used it extensively as of yet but for files and the Google Chrome scraper it looks to be just what I need cheers.
Paul Savage October 13, 2015 -
Glad you found it useful Gerry ! Thanks for stopping by and saying hi.