PHP stripos function

 The PHP stripos function is used to determine the position of the key character in the original string. The first character position of the original string is numbered 0. The usage of the stripos function is the same as the strpos function , but the difference is the stripos function Will not actively distinguish the upper and lower case of English letters, which means that the English letter A and a are the same under the identification of the stripos function. If you need to make a judgment rule that can distinguish the upper and lower case of English letters, please use the strpos function to process It will be easier.


PHP stripos function syntax

int stripos (string $haystack, string $needle, int $offset = 0)


The first parameter of the stripos function, $haystack, is the original string, which is the string to be queried, and the second parameter, $needle, is the key character. Stripost will query $haystack according to $needle. If the matching result is found, it will return to the position. It should be noted that the first character of the original string is counted from 0. If the stripos function does not find a matching result in the original string, It returns false or a non-Boolean value equivalent to false. The third parameter $offset is a selection item, which is used to specify the position from which the stripos function is to be calculated, also known as the offset. The returned key character position is relative to the original string position.

PHP stripos function example
<?php
echo stripos('Welcome to wibibi.Have a good day.','w').'<br>';
echo stripos('Welcome to wibibi.Have a good day.','w',2) ;
?>
The output of the above example
0
11
The first stripos tries to find in the original string whether there are characters that are the same as the lowercase English letter w, and the result is 0. This is because in the judgment rules of the stripos function, the lowercase English letter w is the same as the uppercase letter. W has the same meaning, so when the first w is found, the position is returned. If you want to be able to judge the difference in case, please use the strpos function . In the second stripos function, the offset parameter is used to skip the first W, because the stripos function compares characters from the second position and finds the second w in the original string , Located at the 11th position from the beginning of the original string.

Post a Comment

0 Comments