Replacing the 1st occurence in a string
i should know this one but don't.
given a string ...
$str = "hello my name is bob."
how would i replace only the first space character
with some default text $default = "###";
so $str would become "hello###my name is bob."
i tried str_replace but it seems it will always replace all occurences
preg_replace() has a limit parameter you could use.
$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
thanks nogdog, you're always rescuing me.
would it be difficult to have php perform a google search, and then parse the results and return only the amount of results google found?
for example a search for coffee mugs returns 6,220,000.
i just want the results not the listings.
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?
$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
$pattern="/of about .*> for </";
$temp=str_replace('of about <b>','',$matches);
$num=str_replace('</b> for <','',$temp);
Last edited by SyCo; 11-19-2008 at 08:03 PM.
thanks that works perfectly, i'll have to do some research into that curl function
cURL is a really neat tool. Remember this is a fairly fragile way to do things but, hey it works (for now)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread