gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:52:37 24 4
gpt4 key购买 nike

我有一个像这样的字符串

$string="abc @def @xyz $ @def @xyz";

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

目前我正在使用

strrpos($string,'@');

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

最佳答案

使用 strrpos 可以获取最后一次出现的位置。更多关于 function.strrpos

为了您的目的,您需要使用$ 展开您的字符串,并为展开数组的第一个index 开始应用strrpos

试试这个:

$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

关于php - 在具有结束索引限制的字符串中查找子字符串最后一次出现的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37130142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com