PHP strstr Function Guide: Find Substrings
The strstr function in PHP is used to search for a specific substring within a string and return that substring along with all the characters that follow it. If the substring is not found, it returns false. The syntax of the function is as follows:
strstr(string $haystack, string $needle, bool $before_needle = false): string|false
Explanation of the parameters:
- $haystack: the string to search
- $needle: the substring to search for
- $before_needle: An optional parameter, if set to true, the function will return the part of the string before the needle.
Original: 我需要一杯咖啡。
Paraphrased: I need a cup of coffee.
$str = "Hello, world!";
echo strstr($str, "world"); // 输出:world!
echo strstr($str, "world", true); // 输出:Hello,
In the example above, the strstr function is used to look for the substring “world” in the string $str, and returns the found substring and all characters following it. When the function is called a second time with the third parameter set to true, it returns the portion of the string before the substring “world”.