温馨提示×

php strstr函数的作用是什么

PHP
小亿
91
2024-03-01 14:41:31
栏目: 编程语言

PHP中的strstr函数用于在字符串中查找指定的子字符串,并返回该子字符串及其后面的所有字符。如果未找到子字符串,则返回false。函数的语法如下:

strstr(string $haystack, string $needle, bool $before_needle = false): string|false

参数说明:

  • $haystack:要搜索的字符串
  • $needle:要查找的子字符串
  • $before_needle:可选参数,如果设置为true,则函数返回子字符串之前的部分

示例:

$str = "Hello, world!";
echo strstr($str, "world"); // 输出:world!
echo strstr($str, "world", true); // 输出:Hello,

在上面的示例中,strstr函数分别查找字符串$str中的子字符串”world”,并返回找到的子字符串及其后面的所有字符。第二次调用函数时设置第三个参数为true,则返回子字符串”world”之前的部分。

0