如何查找带有偏移量的子字符串的数字位置


How to find the numeric position of the substring with an offset?

如果我想检查在我可以使用的某些文本中是否出现字符串,

strpos($some_string,"word");返回字符串的第一个字符位置。

但是,如果我想在文本中搜索字符串在设置的起点和终点之间的出现情况,该怎么办。

所以如果$text = "hello my name is fred blogs and i live in a house"

所以CCD_ 3将给出大约18

如果我有$text = "hello my name is fred blogs and i live have a brother fred as well and a granda called fred as well" 怎么办

现在我知道这个字符串中的第二个"fred"位于位置A&那么你在这两个位置之间strpos吗?

如果你知道点A和B的位置,就可以使用

strpos(substr($text,POINT A POSITION,POINT B POSITION),"fred");

strpos()将偏移量作为第三个参数,因此可以执行以下操作:

strpos($text, "fred", 16);

您可以使用第三个参数,即偏移量,在字符串中进一步开始。让它更有活力:

$string = "fred";
strpos($text, $string, strpos($text, $string)+strlen($string));

这将在第一次找到之后找到下一次出现
我使用嵌套的strpos()+strlen()作为第三个参数,使其以偏移开始