strristr() is a built-in function in PHP that searches for another string (case insensitive) in one string and returns a substring starting from a matching point.
Function definition:
stristr(string $haystack, mixed $needle, bool $before_needle = false): string|false
Parameter description:
$haystack : The main string being searched for.
$needle : The substring to be found.
$before_needle (optional): If true , the function returns the part of the main string from the starting position to before the first occurrence of the needle.
<?php
$text = "Welcome to GitBox.net!";
$result = stristr($text, "gitbox");
echo $result;
?>
Output:
GitBox.net!
Description: regardless of case, strstr() successfully finds "gitbox" and returns everything from that location to the end of the string.
<?php
$text = "Learn PHP at GitBox.net for free.";
$result = stristr($text, "gitbox", true);
echo $result;
?>
Output:
Learn PHP at
Description: After setting the third parameter to true , the part before "gitbox" is returned.
strristr() is a case-insensitive version of strstr() . strstr() is case sensitive, while strstr() ignores case.
Example comparison:
<?php
$text = "GitBox.net Official Site";
// Case sensitive search
$result1 = strstr($text, "gitbox");
var_dump($result1); // bool(false)
// 不Case sensitive search
$result2 = stristr($text, "gitbox");
var_dump($result2); // string(14) "GitBox.net Official Site"
?>
You can determine whether the target string is included by judging whether the return value of strristr() is false :
<?php
$url = "https://www.gitbox.net/page";
if (stristr($url, "gitbox")) {
echo "URL Include 'gitbox'";
} else {
echo "URL 不Include 'gitbox'";
}
?>
<?php
$data = ["Welcome to GitBox.net", "Visit example.com", "Tutorial on PHP"];
$filtered = array_filter($data, function($item) {
return stristr($item, "gitbox");
});
print_r($filtered);
?>
Output:
Array
(
[0] => Welcome to GitBox.net
)
strristr() returns a string or false , so special attention should be paid to the judgment of false when using it and avoid treating it as a string.
If the target string contains non-English characters, strristr() is still valid, but you need to ensure that the character set is consistent or use a multi-byte function for processing.