|
may result in unexpected problems. For example, the following script will incorrectly report that the string "THRee" cannot be found inside the string "THRee merry men":
<?php
$haystack = 'Three merry men';
$pos = strpos ($haystack, 'Three');
if (!$pos)
echo "String not found\n";
else
echo "String found at position $pos\n";
?>
Although strpos() performs its search left-to-right, it is possible to start searching from the end of a string and move backward using the strrpos function. Unlike strpos(), however, strrpos() is able to search for only one character. If you specify a string with more than one character as the $needle parameter, only the first character will be considered.
As you can imagine, strpos() is case sensitive, so that, for example, it wouldn't have been able to find the word "three" in the preceding example.
Interestingly, there is no non-case-sensitive alternative to strpos(). However, PHP provides the strstr function, which offers a functionality that is similar to strpos() and provides a non-case-sensitive variant called stristr().
Unlike strpos(), strstr() actually returns the portion of $haystack that succeeds $needle. The following script, for example, will return String found: merry men:
Tags:
Add more tags...,
|