Re: php equivalent to asp's instr()
| From: | David Otton | Date: | Thu, 28 Aug 2003 00:18:43 +0000 |
| Subject: | Re: php equivalent to asp's instr() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-160987@lists.php.net to get a copy of this message | ||
On Wed, 27 Aug 2003 14:07:34 -0700, you wrote:
>I don't know why but I've had the darndest time trying to find an easy
>equivalent of asp's InStr() function in PHP. I know there is
>preg_match(), strpos(), and strstr(), but none of them work like I want
>them to.
>
>In fact, except for maybe preg_match(), I can't get any of them to work
>right.
>
>I want a function that will return true if the string is found, and
>false if it is not found.
>
>Like this:*
>
>$needle = "&";
>$haystack = "Belle & Sebastian";
Hmm. Would you confirm that this code snippet doesn't work for you?
<?
$needle = "&";
$haystack = "Belle & Sebastian";
if (strstr ($haystack, $needle))
{
echo ("strstr() worked<br>");
}
if (strpos ($haystack, $needle))
{
echo ("strpos() worked<br>");
}
?>
Because if it doesn't, I think there's a problem with your install.
BTW, strstr and strpos don't return TRUE if a string is found. strpos
returns an index into $haystack, strstr returns a substring of $haystack.
Maybe you're doing this?
if (strpos ($haystack, $needle) == TRUE)
which would (I would assume) fail. Though PHP plays fast and loose with
types, so it might work.