#41013 [Com]: strstr() and stristr() cannot find zero

From: Date: Sat, 07 Apr 2007 00:51:12 +0000
Subject: #41013 [Com]: strstr() and stristr() cannot find zero
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-111181@lists.php.net to get a copy of this message
ID: 41013 Comment by: webmaster at wiedmann-online dot de Reported By: phraje at gmail dot com Status: Open Bug Type: Unknown/Other Function Operating System: linux 2.6.18 & win32 PHP Version: 5.2.1 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2007-04-06 19:04:25] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41013&edit=1

« previous php.bugs (#111181) next »