This question already has an answer here:
I want to parse the amount of hits a certain google search contains and store that in a PHP variable.
Example:
http://www.google.co.uk/search?as_epq=hello
returns a page containing: 'About 1,830,000,000 results' under the search bar.
All I want to take is the 1830000000 bit.
I'm pretty sure I use preg match but am completely useless at reg expressions, never been able to get my head around it.
The closet I have come is:
$adamHTML = file_get_contents('http://www.google.co.uk/search?as_epq=hello');
preg_match('/About/', $adamHTML, $return);
print_r($return);
Which gives me all the instances of 'About'. But how do I grab the number?
Thanks for any help...
</div>
It's bad, really bad. But you can use this.
preg_match('/About ([0-9,]+) results/', $adamHTML, $return);
Try this
<?php
$adamHTML = file_get_contents('http://www.google.co.uk/search?as_epq=hello&hl=en');
preg_match('/About (.*) results/', $adamHTML, $return);
print $return[1];
?>
You can "extract" (not "parse") content using a regex with placeholders, not just a fixed string. The placeholder for numbers is \d+
, you would additionally need commas etc:
preg_match('/About ([\d,]+) results/', strip_tags($adamHTML), $return);
See also Open source RegexBuddy alternatives and Online regex testing for some helpful tools, or RegExp.info for a nicer tutorial.