php indexof函数的使用方法是什么

655Z技术栈 PHP编程 2026年01月12日 5

在 PHP 中,没有内置的 indexof 函数。不过,可以使用 strpos() 函数来查找一个字符串在另一个字符串中第一次出现的位置。

strpos() 函数的使用方法如下所示:

$string = "Hello, world!"; $substring = "world"; $position = strpos($string, $substring); if ($position !== false) { echo "The substring was found at position: " . $position; } else { echo "The substring was not found"; }

上述代码将输出:The substring was found at position: 7。表示子字符串 “world” 在字符串 “Hello, world!” 中第一次出现的位置是第 7 个字符的位置。如果子字符串没有在字符串中找到,则 strpos() 函数将返回 false

提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。

邮箱:yvsm@163.com 微信:yvsm316 QQ:316430983
关闭

用微信“扫一扫”