使用偏移量- php找到任意字符的位置


Find the position of any of a number of characters with an offset - php?

我正试图找到任何一个字符的位置,但需要能够设置一个搜索偏移量。

这正是strpos()的功能,但匹配的是多个字符中的任何一个,而不是单个字符串/字符。如果strpos()接受$needles数组,它将正是我需要的(假设它返回$haystack中最早匹配的位置)。

strpbrk()在匹配一组字符方面完全需要什么,但这不允许偏移,因此我可以在每次成功匹配后沿着字符串移动。

这似乎是一个奇怪的事情从PHP字符串函数缺失,有什么我失踪?

下面是代码摘要:

  while($pos=strpos($el->text(), '*_-!`' ,$el->position())!==FALSE){
      $el->position($pos);
       foreach ($HandlerTypes as $Type){
          // Note: $el->position is modified within this function
          $this->markSpan($Type, $el);
       }
   }

我正在寻找*, _, -, !或'而不是它们的字符串。任何标记都可以在字符串中出现不止一次。

谢谢。

正则表达式的救星!

$s = 'skipbfoobarxarjar';
preg_match_all('~[bxj]~', $s, $m, PREG_OFFSET_CAPTURE, 5);
print_r($m[0]);
结果:

[0] => Array
    (
        [0] => b
        [1] => 8
    )
[1] => Array
    (
        [0] => x
        [1] => 11
    )
[2] => Array
    (
        [0] => j
        [1] => 14
    )