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

时间:2013-12-09 13:37:46

标签: php

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

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

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

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

所以strpos($text,"fred");会给出大约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&位置之间。 B你们在这两个职位之间是strpos吗?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您可以使用第三个参数(偏移量)在字符串中进一步开始。为了使它更具活力:

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

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

开头
相关问题