Exactly, it cannot be done properly using a regular expression.
Here is an example using DOM :
$xml = new DOMDocument();
$xml->loadHTML($html);
$links = $xml->getElementsByTagName('a');
//Loop through each <a> tags and replace them by their text content
for ($i = $links->length - 1; $i >= 0; $i--) {
$linkNode = $links->item($i);
$lnkText = $linkNode->textContent;
$newTxtNode = $xml->createTextNode($lnkText);
$linkNode->parentNode->replaceChild($newTxtNode, $linkNode);
}
It's important to loop backward whenever changes will be made to the DOM.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…