找到了适合我的东西:
function getTagPositions($strBody, $start, $end)
{
preg_match_all('/' . preg_quote($start, '/') . '([\w\s.]*?)'. preg_quote($end, '/').'/im', $strBody, $strTag, PREG_PATTERN_ORDER);
$intOffset = 0;
$intIndex = 0;
$intTagPositions = array();
foreach($strTag[0] as $strFullTag) {
$intTagPositions[$intIndex] = array('start' => (strpos($strBody, $strFullTag, $intOffset)), 'end' => (strpos($strBody, $strFullTag, $intOffset) + strlen($strFullTag)));
$intOffset += strlen($strFullTag);
$intIndex++;
}
return $intTagPositions;
}
$intTagPositions = getTagPositions("hello there hello hello","he","lo");
// returns
Array ( [0] => Array ( [start] => 0 [end] => 5 ) [1] => Array ( [start] => 7 [end] => 17 ) [2] => Array ( [start] => 18 [end] => 23 ) )