I have thrown together a little function to find and replace a string within a block of text but it seems to be draining resources. I figure this is because I am trying to run it on a whole HTML page.
All I really want to do is replace all text except the title tag.
Here is my function:
/**
* Find and replace strings with skip
*
* @param string $haystack
* @param string $needle
* @param int $start
* @param int $skip
*
* @return mixed
*/
function skip_and_replace($haystack, $needle, $start = 0, $skip = 0) {
$count = 0;
while ($pos = strpos(($haystack), $needle, $start) !== false) {
if ($count <= $skip)
continue;
substr_replace($haystack, ' M<sup>c</sup>', $pos, strlen($needle));
$start = $pos+1;
$count++;
}
return $haystack;
}
Can anyone please help with making this function easier on the memory or let me know if there is a better way to achieve my end goal?