Dan1234 Posted February 11, 2009 Share Posted February 11, 2009 Ok so i've used curl to retrieve a website, but i now want to find (and store in a variable so i can do some math with that number) a specific number that is on the i know is on the wesbite and i know where on the website it is but i dont know what the number will be. So i want to get the number of search results from a specific google search, this number obvs changes depending on what search word you use. So how would i go about doing this? This is my curl code: $url = 'http://www.google.ca/search?q=dan'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 10); $html = curl_exec($curl); curl_close($curl); Any help would be great! Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/ Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 preg_match would be the way to go! GOGO REGEX! Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759723 Share on other sites More sharing options...
Dan1234 Posted February 11, 2009 Author Share Posted February 11, 2009 Yes ive seen this as a solution but don't really know too much about its workings! From what ive tried/seen does this not just tell you how many matches have been found? As oppose to actually retrieving the number? Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759732 Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 int preg_match ( string $pattern , string $subject [, array &$matches You set the third parameter to be an array that contains the match(es). Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759735 Share on other sites More sharing options...
Dan1234 Posted February 11, 2009 Author Share Posted February 11, 2009 Okay, that makes sense. But how do i specify what i want to retrieve if i dont know what it will exactly be...? (Results 1 - 10 of about *NUMBER HERE UNKNOWN*) Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759740 Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 You find the (Results ) and grab everything between there. You can then split/explode it at "about" and viola you have your number. Since I am not good at regex, here is an alternative way: <?php $string = "some site that has (Results 1-10 of about 50) ok"; $string = explode("(Results", $string); $string = explode("of about", $string[1]); $string = explode(")", $string[1]); $number = $string[0]; echo $number; ?> Given that there is no html code in that phrase on the site, it should work. Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759749 Share on other sites More sharing options...
printf Posted February 11, 2009 Share Posted February 11, 2009 Something like, untested but it should work... <?php $str = file_get_contents ( 'http://www.google.com/search?q=phpfreaks' ); $regex = '#<b>(\d+)</b> - <b>(\d+)</b> of about <b>([0-9\,]+)</b>#i'; if ( preg_match ( $regex, $str, $out ) > 0 ) { print_r ( $out ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759767 Share on other sites More sharing options...
Dan1234 Posted February 11, 2009 Author Share Posted February 11, 2009 Awesome, just got it working Thanks very much for the help and quick replies! Quote Link to comment https://forums.phpfreaks.com/topic/144782-finding-a-specific-word-on-a-website/#findComment-759777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.