preg_replace() has a limit parameter you could use.
PHP Code:
$text = preg_replace('/ /', '###', $text, 1);
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Well this works but only for sure as of today. You'd have to keep an eye on the format of the HTML surrounding the result, if it changes you're out of luck. Maybe there's an API solution but for a straight up cURL and parse, it'll get a number. I imagine someone can improve upon it?
PHP Code:
<?php
$searchterms='whatever you want';
$url = "http://www.google.com/search?q=".urlencode($searchterms); $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL,$url); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s $result = curl_exec($ch); // run the whole process curl_close($ch);
$pattern="/of about .*> for </"; $found=preg_match($pattern,$result,$matches); $temp=str_replace('of about <b>','',$matches[0]); $num=str_replace('</b> for <','',$temp); echo $num;
?>
Last edited by SyCo; 11-19-2008 at 08:03 PM.
Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.
Bookmarks