There is no such function (AFAIK) in PHP that does what you are looking for, but you can use preg_match_all to get the substring pattern offsets:
$str = "hello world"; $r = preg_match_all('/o/', $str, $matches, PREG_OFFSET_CAPTURE); foreach($matches[0] as &$match) $match = $match[1]; list($matches) = $matches; unset($match); var_dump($matches);
Output:
array(2) { [0]=> int(4) [1]=> int(7) }
Demo
source share