PHP strstr function



 The PHP strstr function can be used to determine the first occurrence of a character in a string and output the string after the key character, and the output string contains the key character. The strstr function will automatically determine the English The case of letters, if you want to have the judgment effect of "not distinguishing the case of English letters", you can use the similar stristr function to handle it. In addition, if you just want to determine whether a certain character or letter is in the string, you can use a relatively fast and low memory usage strpos function . Please look at the usage of PHP strstr function.


PHP strstr function syntax

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


The first parameter of the PHP strstr function, $haystack, can be said to be the original string being queried, which is a required item. The second parameter, $needle, is the character to be compared and is a required item. If $needle is a number, it will be converted to a corresponding decimal ASCII character, and then the content of the $haystack string will be compared. The third parameter $before_needle is a new parameter in PHP 5.3.0. The default value is false and is not a required item. If it is set to true, all strings before $needle appear in the $haystack string will be output, and no Contains $needle.

PHP strstr function example
<?php
$UserEmail ='someone@wibibi.com';
$EmailDomain = strstr($UserEmail,'@');
echo $EmailDomain.'<br>';

$UserName = strstr($UserEmail,'@',true );
echo $UserName;
?>
The output of the above example
@wibibi.com
someone
In the example, we use the strstr function to determine where the little mouse character "@" appears in the string $UserEmail for the first time. The first strstr function only uses two parameters, and the output result is the default value, which is Contains the mouse @ symbol and all the strings behind it. The second strstr function uses the third parameter. We set the third parameter to true to make the output result completely opposite, that is, the string before the little mouse @ symbol, and does not contain the little mouse @ symbol, this parameter It can be used only with PHP 5.3.0 or newer.

Post a Comment

0 Comments