One of the good point of choosing PHP as your language in creating dynamic web applications is its versatility on string processing, like PERL which is the leading language for string processing, PHP provides a powerful functions and capability in dealing with string. I decided to do a collection of useful regular expressions used on web for certain string matching.
Here are the collections of regExpr:
Domain
"/^(http:\/\/)?([^\/]+)/i"
This will extract the host name from the URL
Example implementation
$url = "http://www.knicks49.com/aboutme/";
preg_match("/^(http:\/\/)?([^\/]+)/i",$url, $result);
$host = $result[2];
print $host;
The output is www.knicks49.com. Alternatively you can also use this expression to extract the last two segments of the domain name.
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $result);
print "domain name is: {$result[0]}";
The output will produce domain name is: knicks49.com
(more…)
Bookmark This!