I needed to convert all links to have target blank if they didn’t already have it. I was going to go down the str_pos and str_replace etc but DOMDocument is a much quicker and easier route. See below:
function addTargetBlankToLinks($html)
{
$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query('//a[not(contains(@href, "protected"))]') as $node) {
$node->setAttribute('target', '_blank');
}
return $dom->saveHtml();
}