note 25127 modified in function.strpos by zak
| From: | zak@php.net | Date: | Wed, 11 Sep 2002 20:51:00 +0000 |
| Subject: | note 25127 modified in function.strpos by zak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-36533@lists.php.net to get a copy of this message | ||
In addition to cerium@x2productions.net:
I always use the following function instead of strpos:
function strpos2 ($s, $needle) {
$pos = strpos($s, $needle);
if ($pos === false) return -1;
else return $pos;
}
So I can easily use it the following way:
$pos = strpos2($string, $needle);
if ($pos >= 0) {
// found
}
else {
// not found
}
or also possible:
$pos = strpos2($string, $needle);
for ($i = 0; $pos >= $i; $i++) {
// do until position
}
Teddy
Editor's Note:
By testing explicitly for FALSE using the strict comparison operators (=== and !==), you can get the
same result (and still be able to a faster built-in function :)
i.e.
$pos = strpos($string, $needle);
if (FALSE !== $pos) {
// found
}
else {
// not found
}
--was--
In addition to cerium@x2productions.net:
I always use the following function instead of strpos:
function strpos2 ($s, $needle) {
$pos = strpos($s, $needle);
if ($pos === false) return -1;
else return $pos;
}
So I can easily use it the following way:
$pos = strpos2($string, $needle);
if ($pos >= 0) {
// found
}
else {
// not found
}
or also possible:
$pos = strpos2($string, $needle);
for ($i = 0; $pos >= $i; $i++) {
// do until position
}
Teddy
http://www.php.net/manual/en/function.strpos.php