|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-04-06 19:04 UTC] phraje at gmail dot com
Description:
------------
If a haystack string has a zero (0) as its last character, and the needle is zero, neither strstr() or stristr() will match it. Can be worked around by concatenating a trailing space to the haystack string.
Reproduce code:
---------------
$haystack="this is a string that ends in 0";
$needle=sprintf("%d",0);
if(strstr($haystack,$needle)) { printf("%s.\n",$haystack); }
if(stristr($haystack,$needle)) { printf("%s.\n",$haystack); }
$haystack="this is a string that doesn't end in 0 ";
if(strstr($haystack,$needle)) { printf("%s.\n",$haystack); }
if(stristr($haystack,$needle)) { printf("%s.\n",$haystack); }
$haystack="this is a string that ends in 1";
$needle=sprintf("%d",1);
if(strstr($haystack,$needle)) { printf("%s.\n",$haystack); }
if(stristr($haystack,$needle)) { printf("%s.\n",$haystack); }
Expected result:
----------------
this is a string that ends in 0.
this is a string that ends in 0.
this is a string that doesn't end in 0 .
this is a string that doesn't end in 0 .
this is a string that ends in 1.
this is a string that ends in 1.
Actual result:
--------------
this is a string that doesn't end in 0 .
this is a string that doesn't end in 0 .
this is a string that ends in 1.
this is a string that ends in 1.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 24 08:00:02 2025 UTC |
In the first two cases strtr() or stristr() returns the string "0". A loose boolean comparisons with "0" is FALSE and so the if statement is not executed. In your case you can change the code to: if(false !== strstr($haystack,$needle)) { printf("%s.\n",$haystack); } BTW: better use strpos() in this case.