10

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.

0

7 Answers 7

14

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.

5
  • nice answer but how do i use it?..not really clear on usage. do i just echo out $newTxtNode? or lnkText???
    – jcobhams
    Sep 25, 2013 at 2:20
  • @VyrenMedia Op asked how to replace links by their text content, so at the end of this loop, you have a DOMDocument object with no links. You can use $xml->saveHTML(); to get the whole html result. $lnkText contains the current link text as string, and you might want to trim it.
    – Yann Milin
    Sep 25, 2013 at 14:09
  • thanks a lot for your reply @Yann-Milin I however found a regex solution for this problem.
    – jcobhams
    Sep 25, 2013 at 16:18
  • See below for the regular expression, the statement "it cannot be done properly using a regular expression." seems not to be true.
    – LarS
    Jul 8, 2015 at 14:17
  • What I was trying to say is that any regular expression solution for this is not a good solution. You obviously can run a regular expression query against html text, but it doesn't mean that you should :) interesting read on the subject : here and here
    – Yann Milin
    Jul 9, 2015 at 18:06
11

Then you can try

preg_replace('/<\/?a[^>]*>/','',$Source);

I tried it online here on rubular

3
  • 1
    This is not correct, as it would also strip other tags starting with a like article or address.
    – LarS
    Jul 8, 2015 at 13:49
  • maybe a better regex: preg_replace('/<\s*\/?\sa(?:\s*|\s+[^>])>/', '', $vars['panes']);
    – LarS
    Jul 8, 2015 at 15:00
  • @CSᵠ answer is better for remove even middle text of 'a' tags
    – Sadee
    Sep 29, 2015 at 14:22
6

You are looking for strip_tags().

<?php

// outputs 'google'
echo strip_tags('<a href="http://www.google.com/" target="_blank">google</a>');
5
  • 2
    I need to maintain other tags, I only need to remove anchors.
    – Lior
    May 3, 2011 at 13:35
  • @Lior ah, I see. strip_tags does indeed not do that. There is an implementation in the user contributed notes that may help you: php.net/manual/en/function.strip-tags.php#100054
    – Pekka
    May 3, 2011 at 13:36
  • @Pekka You can pass a second argument to strip_tags() that is a string of "allowable_tags": php.net/manual/en/function.strip-tags.php.
    – Jasper
    Sep 17, 2012 at 16:40
  • @Jasper but that won't help here, will it? He would have to specify all tags that exist in $allowable_tags
    – Pekka
    Sep 17, 2012 at 16:41
  • @Pekka It is unfortunate that you have to blacklist rather than being able to whitelist what tags you want to remove but using some knowledge of that type of content is being parsed you can probably get that blacklist down to a small list.
    – Jasper
    Sep 17, 2012 at 16:46
6

This question has been answered already but I thought I would add my solution to the mix. I like this better than the accepted solution because its a bit more to the point.

$content = 
    preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content);
2
  • 1
    This is nice and simple, can also use $content = preg_replace(array('"<a (.*?)>"', '"</a>"'), array('',''), $content); in case "href" isn't the first attribute in the anchor tag. Oct 11, 2016 at 3:49
  • @DavidThomas great addition! Oct 12, 2016 at 13:49
5

using regex:

preg_replace('/<a[^>]+>([^<]+)<\/a>/i','\1',$html);

1
  • 1
    What if there is an <img...> element inside the anchor elements? May 3, 2011 at 14:18
0

Have a try with:

$str = '<p>paragraph</p><a href="http://www.google.com/" target="_blank" title="<>">google -> foo</a><div>In the div</div>';
// first, extract anchor tag
preg_match("~<a .*?</a>~", $str, $match);
// then strip the HTML tags
echo strip_tags($match[0]),"\n";

output:

google -> foo
0

Much of the regex here did not help me. Some of it removes the content inside the anchor (which is not at all what OP asked for) and not all of the content at that, some of it will match any tag beginning with a, etc.

This is what I created for my needs at work. We had an issue where passing HTML to wkhtmltopdf that had anchor tags (with many data attributes and other attributes) would sometimes prevent the PDF from producing, so I wanted to remove those while keeping the text.

Regex:

/</?a( [^>]*)?>/ig

In PHP you can do:

$text = "<a href='http://www.google.com/'>Google1</a><br>" .
        "<a>Google2</a><br>" .
        "<afaketag href='http://www.google.com'>Google2</afaketag><br>" .
        "<afaketag>Google4</afaketag><br>" . 
        "<a href='http://www.google.com'><img src='someimage.jpg'></a>";
echo preg_replace("/<\/?a( [^>]*)?>/i", "", $text);

Outputs:

Google1<br>Google2<br><afaketag href='http://www.google.com'>Google2</afaketag><br><afaketag>Google4</afaketag><br><img src='someimage.jpg'>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.