Search for a specific string !!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
l_kris06
Forum Newbie
Posts: 3
Joined: Tue Feb 05, 2008 5:58 am

Search for a specific string !!

Post by l_kris06 »

    Hi all,

    Can somebody here help me out with this please?

    I have a string $temp = "word1 word2 word3 word4 word5 word6 word7....etc"
    i need only "word2 word3 word4" to be in a variable, the count maybe more...basically
    i need to set a start and end marker and get the string in between.


    any help would be much appreciated.

    Thanks,
    kris
    User avatar
    webspider
    Forum Commoner
    Posts: 52
    Joined: Sat Oct 27, 2007 3:29 am

    Re: Search for a specific string !!

    Post by webspider »

    Code: Select all

     
     
    $temp = "word1 word2 word3 word4 word5 word6 word7";
    $findWord = "word2";
     
    $pos = strpos($temp, $findWord);
     
    echo substr($temp, $pos);   // or store in variable
     
     
    User avatar
    webspider
    Forum Commoner
    Posts: 52
    Joined: Sat Oct 27, 2007 3:29 am

    Re: Search for a specific string !!

    Post by webspider »

    i need to set a start and end marker and get the string in between.

    Code: Select all

     
    $startPosition = strpos($temp, "word2");
    $endPosition = strpos($temp, "word4");
     
    $newString = substr($temp,$startPosition, $endPosition); //get the string
     
    User avatar
    RobertGonzalez
    Site Administrator
    Posts: 14293
    Joined: Tue Sep 09, 2003 6:04 pm
    Location: Fremont, CA, USA

    Re: Search for a specific string !!

    Post by RobertGonzalez »

    Post Reply