Copy code The code is as follows:
/*
Function to determine whether a string exists
*/
function strexists($haystack, $needle) {
return !(strpos($haystack , $needle) === FALSE);//Note here "==="
}
/*
Test
*/
$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.
// Simple The use of "==" will not work, you need to use "===", because the first occurrence of a is 0
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";
}
// We can search for the character, ignoring anything before the offset
// When searching for characters, you can use the parameter offset to specify the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring , 'a', 1); // $pos = 7, not 0
?>
The above introduces the === operator that needs to be paid attention to when using strpos under expandenvironmentstrings PHP, including the content of expandenvironmentstrings. I hope it will be helpful to friends who are interested in PHP tutorials.