温馨提示×

php strstr函数的用法是什么

PHP
小亿
82
2024-01-02 13:56:42
栏目: 编程语言

php strstr函数是用于在字符串中查找指定的字符串,并返回从指定字符串开始到字符串末尾的部分。

函数的用法如下:

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

参数说明:

  • haystack:要在其中进行查找的字符串。
  • needle:要查找的字符串。
  • before_needle:可选参数,指定是否返回 needle 之前的部分。默认为 false,返回 needle 开始到字符串末尾的部分。
  • 返回值:如果找到 needle,则返回从 needle 开始到字符串末尾的部分;如果未找到,则返回 false。

示例:

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

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

在上面的示例中,strstr($str, 'world')会找到字符串中第一次出现的 ‘world’,并返回从 ‘world’ 开始到字符串末尾的部分。而 strstr($str, 'world', true) 则会返回 ‘world’ 之前的部分。

0