#30158 [Bgs]: strpos doesn't work as it should
| From: | rasmus@php.net | Date: | Mon, 20 Sep 2004 13:34:04 +0000 |
| Subject: | #30158 [Bgs]: strpos doesn't work as it should | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-66452@lists.php.net to get a copy of this message | ||
ID: 30158
Updated by: rasmus@php.net
Reported By: a dot bendilas at zefxis dot gr
Status: Bogus
Bug Type: Strings related
Operating System: WinXP SP2
PHP Version: 4.3.8
New Comment:
No, it means you should do a !== false check. Anything except false
means it found it.
Previous Comments:
------------------------------------------------------------------------
[2004-09-20 09:12:01] a dot bendilas at zefxis dot gr
Apparently the PHP manual isn't so clear on how to use the ===
operator.
So, what it really means is that you should check for something like
this:
$string = 'time is money';
$srchstring = 'time';
$position = strpos($string, $srchstring);
if (($position > 0) || ($position === 0)) echo 'Search string is
included in the original string!'; else echo 'Search string is not
included in the original string!';
------------------------------------------------------------------------
[2004-09-20 08:53:42] a dot bendilas at zefxis dot gr
A note from the PHP manual on strstr:
If you only want to determine if a particular needle occurs within
haystack, use the faster and less memory intensive function strpos()
instead.
A warning from the PHP manual on strpos:
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE, such as 0 or "". Please
read the section on Booleans for more information. Use the === operator
for testing the return value of this function.
As for why I'm doing a check for true, it's because '0' evaluates to
false.
If my original string was 'time is money' then $position would be 0:
Reproduce code:
---------------
$string = 'time is money';
$srchstring = 'time';
$position = strpos($string, $srchstring);
echo 'String: '.$string.'<br />';
echo 'Search string: '.$srchstring.'<br />';
echo 'Position of search string: '.$position.'<br />';
if ((strpos($string, $srchstring) === true)) echo 'Search string is
included in the original string!'; else echo 'Search string is not
included in the original string!';
Expected result:
----------------
Search string is included in the original string!
Actual result:
--------------
String: time is money
Search string: time
Position of search string: 0
Search string is not included in the original string!
------------------------------------------------------------------------
[2004-09-20 00:18:56] rasmus@php.net
Why are you doing a === to true? strpos is clearly documented to
return a integer specifying the position in the string on a match or
false on a failure. It will never return true.
------------------------------------------------------------------------
[2004-09-19 22:09:19] a dot bendilas at zefxis dot gr
Description:
------------
Although strpos returns the position of the needle correctly, when
testing the return value with a boolean operator to see if it's true,
it produces the opposite result.
Reproduce code:
---------------
$string = 'Once upon a time in America';
$srchstring = 'time';
$position = strpos($string, $srchstring);
echo 'String: '.$string.'<br />';
echo 'Search string: '.$srchstring.'<br />';
echo 'Position of search string: '.$position.'<br />';
if ((strpos($string, $srchstring) === true)) echo 'Search string is
included in the original string!'; else echo 'Search string is not
included in the original string!';
Expected result:
----------------
Search string is included in the original string!
Actual result:
--------------
Search string is not included in the original string!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30158&edit=1