查询

stristr()函数—用法及示例

「 在字符串中查找子字符串,并返回从子字符串开始到字符串末尾的所有字符 」


函数名:stristr() 

适用版本:所有版本

用法:stristr()函数用于在字符串中查找子字符串,并返回从子字符串开始到字符串末尾的所有字符。该函数是大小写不敏感的。

语法:stristr(string $haystack, mixed $needle, bool $before_needle = false): string|false

参数:

  • $haystack:要查找的字符串。
  • $needle:要查找的子字符串。
  • $before_needle(可选):设置为true时,返回子字符串之前的部分;设置为false(默认),返回子字符串开始到字符串末尾的所有字符。

返回值:

  • 如果找到了子字符串,则返回从子字符串开始到字符串末尾的所有字符。
  • 如果未找到子字符串,则返回false。

示例:

$string = "Hello World!";
$substring = "WORLD";
$result = stristr($string, $substring);
echo $result; // 输出:World!

$string = "Hello World!";
$substring = "WORLD";
$result = stristr($string, $substring, true);
echo $result; // 输出:Hello

在第一个示例中,函数将忽略大小写,找到了子字符串"WORLD",并返回从子字符串开始到字符串末尾的所有字符"World!"。

在第二个示例中,通过设置$before_needle参数为true,函数返回子字符串之前的部分"Hello"。

补充纠错
上一个函数: strlen()函数
下一个函数: strncmp()函数
热门PHP函数
分享链接