<?php

$html = <<<EOF
<p>Hi Hello <a href="#">World</a></p>.
This is <div class="myclass">testing <a href="#">content</a>. some more content</div>.
One more <a href="#"> Link </a>.
EOF;

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXpath($dom);

$links = $xpath->query("//a[not(ancestor::div[@class='myclass'])]");

foreach ($links as $link) {
	$link->parentNode->removeChild($link);
}

// just to test it out
echo $dom->saveHTML();
?>