tags:

views:

59

answers:

3

Hi, My company has a series of websites with very similar domain names based on the city. The format for these is http://locksmithdallas.com or http://locksmithgarland.com. I'd like to figure out a way to interpret Dallas and Garland out of the domain names in php. I'm a noob at regex, so I could really use some help!

+3  A: 
if (preg_match('/locksmith([^.]*)\.com/', $url, $matches)) {
    $city = $matches[1];
}
Gavin Miller
+1  A: 
if (preg_match('#locksmith([^.]*)\.com#', $url, $matches)) {
    //has been found
    var_dump($matches);
}
André Hoffmann
+1  A: 

This one's simple but you can elaborate from there:

http://locksmith(?<name>[a-zA-Z]+)\.com

That will give you a named group called "name" where you can grab the text after locksmith and before .com.

HackedByChinese
Thanks for that, I'm still learning and examples are one of the best ways to do it!