PHP函数 strstr() 和 stristr() 有什么区别

在 PHP 中,strstr()stristr() 都是用于在一个字符串中查找另一个字符串的首次出现,并返回从该位置到字符串末尾的子字符串。不过,它们在处理大小写方面有所不同。

strstr()

  • 大小写敏感strstr() 是大小写敏感的,即它会区分大小写来查找子字符串。

  • 语法

    php 复制代码
    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) : string|false
  1. $haystack:要搜索的字符串(也称为"haystack")。
  2. $needle:要查找的子字符串(也称为"needle")。
  3. $before_needle(可选):如果设置为 true,则返回 needlehaystack 中出现之前的部分。默认值是 false
  • 示例

    php 复制代码
    $haystack = "Hello, World!";  
    $needle = "World";  
    echo strstr($haystack, $needle); // 输出 "World!"

stristr()

  • 大小写不敏感stristr() 是大小写不敏感的,即它不会区分大小写来查找子字符串。

  • 语法

    php 复制代码
    string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) : string|false
    1. 参数与 strstr() 完全相同。
  • 示例

    php 复制代码
    $haystack = "Hello, World!";  
    $needle = "world";  
    echo stristr($haystack, $needle); // 输出 "World!"

总结

  • 大小写敏感性
    • strstr() 是大小写敏感的。
    • stristr() 是大小写不敏感的。
  • 用法
    • 如果你需要区分大小写来查找子字符串,请使用 strstr()
    • 如果你不需要区分大小写来查找子字符串,请使用 stristr()
相关推荐
JaguarJack6 小时前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo6 小时前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack21 小时前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay2 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954482 天前
CTF 伪协议
php
BingoGo4 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack4 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo5 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack5 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack6 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端