很多可能性。例如,使用
DOM extension
,
DOMDocument::loadhtml()
和
XPath
(尽管在这种情况下getElementsBytagName()就足够了)。
<?php
$string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><html><head><title>...</title></head><body>
<p>
mary had a <a href="little">greedy</a> lamb
whose fleece was <a href="white">cold</a> as snow
</p>
</body></html>';
$doc = new DOMDocument;
$doc->loadhtml($string);
$xpath = new DOMXPath($doc);
foreach( $xpath->query('//a') as $a ) {
$tn = $doc->createTextNode($a->getAttribute('href'));
$a->parentNode->replaceChild($tn, $a);
}
echo $doc->savehtml();
印刷品
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>...</title></head>
<body><p>
mary had a little lamb
whose fleece was white as snow
</p></body>
</html>