PHP strpos function
The PHP strpos function is used to find the numeric position of the key character in the original string. For example, using the strpos function to query the numeric position of the letter e in Hello, it will return a result like 1, and the first letter starts from 0 Calculation. The strpos function will automatically determine the case of key characters in English letters, that is to say, A and a are different characters under the identification of the strpos function. If you want to make the effect of "insensitive to the case of English letters", Please use the stripos function to determine the key character.
Basic syntax of PHP strpos function
int strpos( string $haystack, string $needle, int $offset)
The first parameter $haystack of the strpos function represents the original string, which is the string to be inspected. It is a required item. The second parameter $needle is the key character. The strpos function will go according to $needle. Query whether $haystack contains a compatible string of related constructions. If there is, it will return to the position of the number (starting character is counted from 0). If there is no matching character, it will return false or its equivalent value. The non-Boolean value of. The third parameter $offset is an optional item, used to specify the position from which to start the calculation, also called the offset. The returned result position is relative to the starting position of $haystack. Please refer to Example 2.
PHP strpos function example 1
$NewString='Welcome to wibibi.Have a good day.';
$FindKey='w';
$TryStrpos=strpos('Welcome to wibibi.Have a good day.','w');
if($ TryStrpos === false){ echo'no
find';
}else{
echo'The key character w is at the first position of the original string'.$TryStrpos.';
}
?>
PHP strpos function example 2: using offset
echo strpos('Welcome to Wibibi.Have a good day.','W').'<br>';
echo strpos('Welcome to Wibibi.Have a good day.','W',2) ;
?>
11
Post a Comment
0 Comments