温馨提示×

php stristr函数的用法是什么

PHP
小亿
82
2024-04-09 17:49:39
栏目: 编程语言

stristr函数是PHP中用来查找字符串中第一次出现的子字符串(不区分大小写)的函数。其语法为:

stristr(string $haystack , string $needle , bool $before_needle = false): ?string

参数说明:

  • $haystack:要查找的字符串
  • $needle:要查找的子字符串
  • $before_needle:可选参数,设置为true将返回子字符串之前的部分,否则返回子字符串及其后的部分

示例:

$string = 'Hello, World!';
$substring = 'world';

$result = stristr($string, $substring);

echo $result; // 输出: World!

在上面的示例中,由于stristr函数对大小写不敏感,所以即使$substring的大小写与实际字符串不一致,仍然能够找到对应的子字符串。

0