PHP stristr function

 The PHP stristr function can determine the position of the special character in the string for the first time, and output the key characters and other strings on the web page. The usage is the same as the strstr function , but the difference is that the strist function will size the English letters Writing is considered the same, and the strstr function treats letter case as different.


Basic syntax of PHP stristr function

string stristr ( string $haystack , $needle , bool $before_needle )


The first parameter $haystack of the stristr function is the original string to be queried, and the second parameter $needle is the self-defined key character. The stristr function will automatically determine whether there is a $needle character in the $haystack string , If any, return the string containing the $needle character and other characters after it. These two parameters are required items. If stristr does not find any key characters in the original string, it will return false. The last parameter is a new parameter after PHP 5.3.0, optional item, default is false, if set to true, the stristr function finds a key character in the original string and returns the string before the character , And does not contain key characters.

PS.$needle parameters can also use numbers, the stristr function will convert them into the corresponding decimal ASCII characters.

PHP stristr function example 1
<? php
$ UserEmail = 'someone@wibibi.com';
echo stristr ($ UserEmail, 'E');
echo stristr ($ UserEmail, 'E', true);
?>
The output of the above example
eone@wibibi.com
som
From this example, we can see that stristr compares the key characters regardless of the case of English letters. The second output uses the third parameter, which must be effective in PHP 5.3.0 or above. This example can be compared Right : PHP strstr function .

PHP stristr function example 2
<?php
$UserEmail  = 'someone@wibibi.com';
echo stristr($UserEmail, 101).<br>;
echo stristr($UserEmail, 69);
?>
The output of the above example
eone@wibibi.com
eone@wibibi.com
In the second example, we use the numbers "101" and "69" in the second parameter, where "101" is converted to an ASCII decimal English letter as e, and the result of "69" is converted to E, and then go through the stristr function After checking, you can output the results smoothly.

PHP stristr function example three
<?php
$UserEmail ='someone@wibibi.com';
if(stristr($UserEmail,'z')===FALSE){ echo'The
 English letter z is not in the original string';
}
?>
The output of the above example
The English letter z is not in the original string
This example mainly uses the characteristics of the stristr function. When no key character is found in the original string, it returns false. Using this trick, a small model for judging whether a specific keyword exists in the string is made This function can also directly use the built-in PHP strpos function .

Post a Comment

0 Comments