[PHP] Problems with function using strrpos() and substr(), plz help...

Discussion in 'Programming & Software Development' started by SpeedFreak, Jul 26, 2003.

  1. SpeedFreak

    SpeedFreak Member

    Joined:
    Jun 27, 2001
    Messages:
    1,385
    Location:
    London, UK | OCAU's Euro corrispondant!
    hey,

    i have this code:


    code:--------------------------------------------------------------------------------
    if (preg_match("#<!--INTRO-->#", $row['post']))
    {
    $istart = strrpos($row['post'] , '<!--INTRO-->' ); //position of intro
    $isend = strrpos($row['post'] , '<!--END-INTRO-->' ); //position of end intro

    $iend = $isend-$istart; //difference integer

    $extract = substr($row['post'], $istart, $iend);
    }
    else {
    $extract = $row['post'];
    }
    --------------------------------------------------------------------------------


    the else{} part works fine but the substr() bit doesnt as $extract is being returned empty when the preg_match statement returns true.

    what have i done wrong?

    Cheers
     
  2. Gumby

    Gumby Member

    Joined:
    Jun 27, 2001
    Messages:
    1,743
    Location:
    Brisbane
    echo some error checking lines.
    ensure that its getting $istart and $isend properly, and that its calculating $iend.
     
  3. OP
    OP
    SpeedFreak

    SpeedFreak Member

    Joined:
    Jun 27, 2001
    Messages:
    1,385
    Location:
    London, UK | OCAU's Euro corrispondant!
    so just echo each variable as the script goes on to check if its recieving it right?

    i'll give it a go :)
     
  4. Bangers

    Bangers Member

    Joined:
    Dec 25, 2001
    Messages:
    7,254
    Location:
    Silicon Valley
    Give $istart, $iend static numbers to see if they are causing the problem. My guess is that they are, but I wouldn't touch PHP with a 40ft pole so I'm sure.

    My guess is that both $istart, $iend are 0. Echo those variables out.
     
  5. OP
    OP
    SpeedFreak

    SpeedFreak Member

    Joined:
    Jun 27, 2001
    Messages:
    1,385
    Location:
    London, UK | OCAU's Euro corrispondant!
    fixed :)

    i was using strrpos() which only allows a single character as the needle :rolleyes: D'oh!

    i needed to use strpos(), except a mate of mine showed me a more efficient way of doing it like so:
    PHP:
    if (preg_match("/(.*)($start)(.*)($end)(.*)/i"$row['post'], $yourtext)) {
                  
    $extract $yourtext[3];
                
    $extra "...<br><br><font size='2' face='verdana'><b><a href='" $ibforums->base_url "?act=ST&f={$row['forum_id']}&t={$row['tid']}'>[Read the Full Article...]</a></b></font>";
            }
            else { 
                  
    $extract $row['post']; 
                
    $extra "";
            }
    All this was part of a mod i was writing for Invision Board ssi.php to get more control over news exporting with a defined intro and full article section. I modded the post parser aswell to tell it to turn [intro] tags into <!--INTRO--> and <!--END-INTRO-->. This has no effect in the forums, but the ssi.php file extracts only the intro (if it exists).

    The fruit of this can be seen on my latest creation: www.ps2evolution.com

    thanks to all who helped :)
    (this mod is prolly gonna be included in the next release of invision)

    Cheers
     

Share This Page

Advertisement: