Find first occurence of substring before a certain position
In PHP, if I have a long string, IE 10'000 chars, how would you suggest I go about finding the first occurence of a certain string before and after a given position.
IE, if I have the string:
BaaaaBcccccHELLOcccccBaaaaB
I can use strpos to find the position of HELLO. How could I then go about finding the position of the first occurence of B before HELLO and the first occurence of B after HELLO?
Given the position…
To find the first occurrence before, you can take the substr()
before the match and use strrpos()
.
To find the first occurrence after, you can still use strpos()
and set the offset parameter.
上一篇: 在startx后自动运行脚本
下一篇: 在某个位置之前查找子字符串的第一次出现