Hi Chris,

I've always used strstr() for this _exact_ same purpose and it's worked just
fine.  I find it much more useful as an InStr()-like function than for
actually doing anything with the string it returns.  You'll want to note
that the parameter order is $haystack before $needle.  That goes for
strpos() as well.  I noticed that in your original post, you're using a
hypothetical function FoundInString($needle,$haystack).  If you used the
same order ($needle,$haystack) when you tried strstr() or strpos(), then it
would fail because it's a bit difficult to find a haystack in a needle. =/

I _strongly_ recommend using one of these built-in functions, as they will
run much faster than any programmer-defined function.  Below I will explain
how and potential pitfalls.

Only problem I noticed is that you want the function to return an explicit
true if the string is found (and false if it is not).  Both functions will
return an explicit false if the needle is not found, but neither return an
explicit true.  Don't let that stop you from using them though.  If needle
is found, strstr() returns a string and strpos() returns an integer
(possibly 0).  An if statement will consider a string (as returned by
strstr()) as logically true, so you can use strstr() by itself inside an if
statement.  strpos() however returns the offset from the beginning of
$haystack where it found $needle.  This means that if $needle is at the
beginning of $haystack, then it will return an integer 0.  This of course is
considered false by an if statement.  However, you can use
if(strpos($haystack,$needle)!==false).  If you take a look at
http://us3.php.net/manual/en/language.operators.comparison.php, you'll see
that PHP's $a===$b and $a!==$b operators result in a true only if $a and $b
are of the same type (bool, int, etc).  Since an integer 0 is not the same
as a boolean false, strpos()!==false will not choke when it returns an
integer 0.

If you want the function to return an explicit true because you want to
assign the result to a variable, you can do this:
$foo = (strstr($haystack,$needle)!==false)
or anywhere else you plan to use it for that matter.

Documentation for strstr() and strpos()
http://us3.php.net/strstr
http://us3.php.net/strpos

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to