Page 1 of 1

finding lines ending in ")" with substr()

Posted: Fri Apr 24, 2009 12:29 pm
by akronymn
I'm running into an odd situation using substr(). I am trying to find all lines in a text file that end in a ")" or more accurately ")\n" using substr(). First I read in a line and then explode it into an array called $words. Then when I try

Code: Select all

 
if(substr($words[count($words) -1], -1) == "\n")
{...}
 
This returns all lines in the file as expected. But when I try

Code: Select all

 
if(substr($words[count($words) -1], -2) == ")\n")
 
or

Code: Select all

 
if(substr($words[count($words) -1], -2, 1) == ")")
 
It returns none of the lines and I'm not seeing why. I've tried several similar combinations all with no luck. Can anyone spot what I am doing wrong? Thanks! -adam

Re: finding lines ending in ")" with substr()

Posted: Fri Apr 24, 2009 12:56 pm
by akronymn
Update: Figured it out...

so the solution was to use

Code: Select all

 
if(substr($words[count($words) -1], -3, 1) == ")")
 
apparently the \n counts as both 1 and 2 characters... interesting.

Re: finding lines ending in ")" with substr()

Posted: Fri Apr 24, 2009 2:58 pm
by John Cartwright
Or you could trim() the string prior to searching it.