Why won't my strpos command work?

Discussion in 'PHP' started by Imozeb, Mar 9, 2010.

  1. #1
    Why won't my strpos command work. My goal is to read the data in between the first set of brackets. For $titlestart it returns the correct value, but for $titleend it returns a value that is about five greater than what I assumed it to be. I assumed it to be the value of the close bracket.

    I don't think there are any glitches in the file part.

    Please tell me where I messed up.

    Here's my code:

    PHP code:
    $titlestart = (strpos($filedata,'(') + 1);
    $titleend = strpos($filedata,')');
    $titlename = substr($filedata,$titlestart,$titleend);
    Code (markup):
    txt file code:
    Name(Testtitle)  |  Owner(TestOwner)  |  Description(Test Test Test.)  |  # 
    LINE1
    Code (markup):
    Thanks!

    ~imozeb :)
     
    Imozeb, Mar 9, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Use a negative length on your substr()

    <?php
    
    $filedata = <<<eof
    Name(Testtitle)  |  Owner(TestOwner)  |  Description(Test Test Test.)  |  # 
    LINE1
    eof;
    
    $titlestart = strpos($filedata,'(');
    $titleend = strpos($filedata,')');
    $titlename = substr($filedata,$titlestart,$titleend -1);
    
    echo $titlename;
    
    ?>
    PHP:

    "If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned. "

    or even...

    <?php
    
    $filedata = <<<eof
    Name(Testtitle)  |  Owner(TestOwner)  |  Description(Test Test Test.)  |  # 
    LINE1
    eof;
    
    $titlestart = strpos($filedata,'(');
    $titleend = strpos($filedata,')') -1;
    $titlename = substr($filedata,$titlestart,$titleend);
    
    echo $titlename;
    
    
    ?>
    PHP:
     
    danx10, Mar 9, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you tell me exactly why my code isn't working because if I am reading it right, it gets the beginning and ending locations of the brackets and then cuts the string so that the new string is equal to the data within the brackets. What did I do wrong? Is it something like escaping brackets?

    I need to know cause I have a few more of this type of code blocks.


    For example my second part isn't working now and there is a third code block using data from the second one to get the description.

    PHP Code:
    
    $strresponse = substr($filedata,($titleend),strlen($filedata));
    $ownerstart = strpos($strresponse,'(') + 1;
    $ownerend = strpos($strresponse,')');
    $ownerdata = substr($strresponse,$ownerstart,$ownerend);
    
    Code (markup):
    Thanks.

    ~imozeb
     
    Last edited: Mar 9, 2010
    Imozeb, Mar 9, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've been banging my head against a wall for another two hours trying to figure out whats wrong. Could someone just tell me what's wrong. My heads starting to hurt. :)
     
    Imozeb, Mar 10, 2010 IP
  5. noface83

    noface83 Peon

    Messages:
    8
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hope this help.

    
    $filedata = "Name(Testtitle)  |  Owner(TestOwner)  |  Description(Test Test Test.)  |  # LINE1";
    
    $titlestart = strpos($filedata, '(');
    $titleend = strpos($filedata, ')', $titlestart);                             
    $title = substr($filedata, $titlestart+1, $titleend-$titlestart-1);
    
    $ownerstart = strpos($filedata, '(', $titleend);
    $ownerend = strpos($filedata, ')', $ownerstart);
    $owner = substr($filedata, $ownerstart+1, $ownerend-$ownerstart-1);
    
    $descstart = strpos($filedata, '(', $ownerend);
    $descend = strpos($filedata, ')', $descstart);
    $desc = substr($filedata, $descstart+1, $descend-$descstart-1);
    
    echo $title.' '.$owner.' '.$desc;
    
    Code (markup):
    If you need to include "(" then remove +1, if you need to include ")" then remove -1.
     
    noface83, Mar 16, 2010 IP