private static function replace($selector, $replace, $html, $replace_inner = false, &$matches = NULL)
{
$start_from = 0;
$limit = 300;
$data = false;
$replace = (array) $replace;
do {
$data = self::find($selector, $html, $start_from);
if ($data) {
$r = array_shift($replace);
// from where to where will we be replacing?
$replace_space = $replace_inner ? $data['inner_end'] - $data['inner_start'] : $data['outer_end'] - $data['outer_start'];
$replace_len = strlen($r);
if ($matches !== NULL) {
$matches[] = substr($html, $replace_inner ? $data['inner_start'] : $data['outer_start'], $replace_space);
}
$html = substr_replace($html, $r, $replace_inner ? $data['inner_start'] : $data['outer_start'], $replace_space);
// next time we resume search at position right at the end of this element
$start_from = $data['outer_end'] + ($replace_len - $replace_space);
}
} while ($data && --$limit > 0);
return $html;
}