Return all results using preg_match

0

I need to return some results of the "ex: Digital Agency" search of the first page of Google using preg_match + RegEx, but it is not returning all 10 values in the array, only the first. How do I solve it?

Ex:

$document = file_get_contents('https://www.google.com.br/search?q=Ag%C3%AAncia+Digital');

preg_match_all('/<li class=\"g\">([^']*?)<\/li>/', $document, $matches);

print_r($matches);
    
asked by anonymous 21.05.2014 / 18:18

1 answer

0

When you syntactically analyze the HTML of a page with Regular Expressions, unfortunately being specific may be a problem.

In these cases you should capture everything that is within a specific content delimiter, in your case the <li></li> . And because of ? in your group you undo the gulodice of the * quantizer.

To the letter it's as if you're saying: Anything other than a backtick, in any amount, but if you do not have any, that's fine too .

So much that it was enough to remove the interrogation and all the other results appeared.

    
21.05.2014 / 18:41