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
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:
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
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.
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.