扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

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

扬州沐宇科技
2024-03-07 14:55:23
PHP

在 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

扫码添加客服微信