Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove anchors from text

Tags:

regex

php

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.

like image 686
Lior Avatar asked May 03 '11 13:05

Lior


People also ask

How do you Unanchor text in Word?

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".

How do I release the anchor in Word?

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.

How do you Unanchor an object?

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.


1 Answers

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.

like image 79
Yann Milin Avatar answered Oct 13 '22 05:10

Yann Milin