PHP - strpos - 搜索所有字符串,没有从X到Y的偏移量


PHP - strpos - search in all string without offset from X to Y

在strpos函数中,我们可以定义偏移量 - 解析器sholud开始在字符串中搜索我们的子字符串。我必须创建类似的东西 - 我必须从字符串中删除范围偏移量,确切地说:

$string = 'This is my string';
echo strpos($string, 'is my', 0);

它将返回主$string中子字符串is my的位置。但是如何告诉 PHP 脚本搜索所有字符串(如上面的脚本)但不在 X 到 Y 的位置上?可能吗?

注意:

我正在使用mb_strpos()但我认为,这两个功能的解决方案将非常相似。

谢谢。

你需要你的自定义函数 - 它应该适合你:

//$X<$Y
funciton getOffset($str, $needle, $X, $Y) {
    $str1 = substr($str, 0, $X);
    $str2 = substr($str, $Y);
    $pos1 = strpos($str1, $needle, 0);
    $pos2 = $Y + strpos($str1, 'is my', 0);
    return  false == $pos1 ? $pos2 : $pos1;
}