How to find the position of end of a string?

Discussion in 'PHP' started by bobby9101, May 30, 2007.

  1. #1
    Hi, say i have a string that looks like:

    //Comment1 //
    Hey look
    //Comment2 //
    Now look

    How do I get the position number of the end of "//Comment1 //"
    BTW the length of the string is variable, so I can't count from the start.
     
    bobby9101, May 30, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?
    $string = '//Comment1 //
    Hey look
    //Comment2 //
    Now look';
    
    function strpostend( $haystack, $needle, $offset = null )
    {
    	return strpos( $haystack, $needle, $offset ) + strlen( $needle );
    }
    
    $test1 = 'Comment1 //';
    printf('%s ends at offset %d<br />', $test1, strpostend( $string, $test1 ) );
    printf('The rest of the string is<br /><pre>%s</pre>', substr( $string, strpostend( $string, $test1 ) ) );
    
    PHP:
    like that .....
     
    krakjoe, May 30, 2007 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok with a good bit of modification it works perfectly, thanks
     
    bobby9101, May 30, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    what did you have to modify ??

    I musta misunderstood the question, as I understand it returns the position of the end of the string you pass to it ......
     
    krakjoe, May 30, 2007 IP
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah I posted example code, that wasn't exactly how it was in my real code *oops* so it was my fault.
     
    bobby9101, May 30, 2007 IP