PHP

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

小亿
199
2024-03-07 14:55:23
栏目: 编程语言

在 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

0
看了该问题的人还看了