www.webdeveloper.com
Results 1 to 7 of 7

Thread: Replacing the 1st occurence in a string

  1. #1
    Join Date
    Aug 2006
    Posts
    301

    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

    thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,362
    preg_replace() has a limit parameter you could use.
    PHP Code:
    $text preg_replace('/ /''###'$text1); 
    "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

    eBookworm.us

  3. #3
    Join Date
    Aug 2006
    Posts
    301
    thanks nogdog, you're always rescuing me.

    much appreciated.

  4. #4
    Join Date
    Aug 2006
    Posts
    301
    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.

  5. #5
    Join Date
    Apr 2007
    Posts
    1,664
    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($chCURLOPT_URL,$url); // set url to post to
    curl_setopt($chCURLOPT_FAILONERROR1);
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);// allow redirects
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($chCURLOPT_TIMEOUT10); // 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.

  6. #6
    Join Date
    Aug 2006
    Posts
    301
    thanks that works perfectly, i'll have to do some research into that curl function

  7. #7
    Join Date
    Apr 2007
    Posts
    1,664
    cURL is a really neat tool. Remember this is a fairly fragile way to do things but, hey it works (for now)
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles