如何在 php 中的字符串中获取重复单词数组中的位置列表


How can I get a list of positions in array of repeated word in string in php

假设我有类似 b<a=2<sub>2</sub> && d>c = 4<sub>2</sub> == 6<sub>3</sub>"<子>"字重复3次。如何将所有 3 个位置放入数组中?

尝试如下:

$find = 'b<a=2<sub>2</sub> && d>c = 4<sub>2</sub> == 6<sub>3</sub>';
function strpos_all($haystack, $needle) {
    $offset = 0;
    $allpos = array();
    while (($pos = strpos($haystack, $needle, $offset)) !== FALSE) {
        $offset   = $pos + strlen($needle);
        $allpos[] = $pos;
    }
    return $allpos;
}
print_r(strpos_all($find, "<sub>"));