I need to remove anchor tags from some text, and can't seem to be able to do it using regex.
Just the anchor tags, not their content.
For instance, <a href="http://www.google.com/" target="_blank">google</a>
would become google
.
If you really want to remove the anchor, you do it by selecting the object and deleting it from the document. If you just don't want to see the anchor symbol (but you do want to keep the object), click File > Options > Display and uncheck the box for "object anchors".
The steps for creating hyperlinks are provided in another step-by-step guide. To remove an anchor, place your cursor on the text immediately below the anchor. Then, click Insert/remove anchor in the Links section of the Insert ribbon tab.
Release an anchored object If you no longer want an object to move relative to its associated text, you can release it to remove its anchor. Select the anchored object with a selection tool, and choose Object > Anchored Object > Release.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With