在具有结束索引限制的字符串中查找最后一次出现的子字符串的位置

Uma*_*hid 5 php string

我有一个字符串

$string="abc @def @xyz $ @def @xyz";
Run Code Online (Sandbox Code Playgroud)

现在我想在$之前获得@的最后一次出现的索引.

目前我正在使用

strrpos($string,'@');
Run Code Online (Sandbox Code Playgroud)

strrpos的第三个参数将是起始索引,我们可以给出结束索引吗?

Mur*_*san 5

使用strrpos你可以得到最后一次出现.有关function.strrpos的更多信息

为了您的目的,您需要爆炸您的字符串$并启动strrposindex一个爆炸数组的应用程序.

试试这个:

$string="abc @def @xyz $ @def @xyz";
$strArr = explode('$', $string);
$pos = strrpos($strArr[0], "@");

if ($pos === false) { // note: three equal signs
    echo 'Not Found!';
}else
    echo $pos; //Output 9 this case
Run Code Online (Sandbox Code Playgroud)