Trouble Finding Last Position of String

Discussion in 'PHP' started by BraveStarr, Dec 16, 2007.

  1. #1
    I'm modifying a string and want to find the last position of the occurence of a substring such as "[TEST". This is always followed somewhere in the string with the substring "[/TEST]".

    I've used strrpos($string, "[TEST") and strrchar($string, "[TEST").

    Each time I get the position of the "[/TEST]" string which follows it. I imagine this has something to do with regular expressions possibly... I just want to deal with strings here though and don't seem to be able to...

    Thanks,
    James
     
    BraveStarr, Dec 16, 2007 IP
  2. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have php 5.0 use strripos()

    If not use this...
    function strripos($haystack, $needle)
    {
        $haystack = strtolower($haystack);  //remove this for case-sensitivity
        $needle = strtolower($needle); //remove this for case-sensitivity        
        $tmp  = strpos(strrev($haystack),strrev($needle));
        if ($tmp === false)
            return false;
        else
            return strlen($haystack) - ($tmp + strlen($needle)-1) - 1;
    }
    PHP:
    like this: echo strripos($string, "[TEST");
     
    Gawk, Dec 16, 2007 IP
  3. BraveStarr

    BraveStarr Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Gawk!
     
    BraveStarr, Dec 16, 2007 IP