Forum Moderators: coopster

Message Too Old, No Replies

looking for a string function

         

michal317

8:26 pm on May 31, 2006 (gmt 0)

10+ Year Member



Hi!
I'm looking for a string function that is one string is found in another string (longer one) it will return TRUE.
for example:
look for xyz in abcxyzde
if it's found - than return TRUE.
thanks!

jatar_k

8:30 pm on May 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try
[php.net...]

eelixduppy

8:31 pm on May 31, 2006 (gmt 0)



if($test = strpos("abcxyzde","xyz"))
{ echo "Found 'xyz'"; }
else { echo "Cannot find 'xyz'"; }

[edit]Wow...we keep doing this ;)[/edit]

surfin2u

9:18 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



Not so fast!

if($test = strpos("abcxyzde","abc"))
{ echo "Found 'abc'"; }
else { echo "Cannot find 'abc'"; }

/* above code fails because PHP treats 0 as FALSE */

/* code below works because it checks for FALSE by using === */

if(FALSE === strpos("abcxyzde","abc"))
{ echo "Cannot find 'abc'"; }
else { echo "Found 'abc'"; }

eelixduppy

9:24 pm on Jun 1, 2006 (gmt 0)



Good catch...I wasn't thinking about it returning 0 (finding the string at the first position)...Thanks ;)

surfin2u

10:42 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



That mistake leads to a whole string of hard to track down bugs ;-)