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, 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

// regex
//remove anything inside () in string $into[$row]['city']

$re = "/\\(.*\\)/";
preg_replace($re, '', ($info[$row]['city'], 1)


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 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.


Leave a Reply

Your email address will not be published.