The strpos() function returns the position of the first occurrence of a string inside another string. If the string is not found, this function returns FALSE.
Method Signature
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )haystack : The string to search in.
needle : If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
offset : The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.
Returned Values : Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE.
Examples
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // because the position of 'a' was the 0th (first) character. if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } ?>
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // The !== operator can also be used. Using != would not work as expected // because the position of 'a' is 0. The statement (0 != false) evaluates // to false. if ($pos !== false) { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; } else { echo "The string '$findme' was not found in the string '$mystring'"; } ?>
0 comments:
Post a Comment