I want to use PHP regular expressions to match out all the links which contain the word google. I’ve tried this:
$url = "http://www.google.com";
$html = file_get_contents($url);
preg_match_all('/<a.*(.*?)".*>(.*google.*?)<\/a>/i',$html,$links);
echo '<pre />';
print_r($links); // it should return 2 links 'About Google' & 'Go to Google English'
However it returns nothing. Why?
You should use a dom parser, because using regex for html documents can be “painfully” error prone.
Try something like this