首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]stristr()函数—用法及示例

发布于 2025-05-08 19:28:36
0
11

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

函数名: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"。

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

8926

帖子

29

小组

2863

积分

站长交流