Most of the regex didn't help me here. Some of them delete the contents inside the anchor (which is not at all what the OP requested), and not all the contents in it, some of them will match any tag starting with a, etc.
This is what I created for my needs at work. We had a problem when transferring HTML to wkhtmltopdf with anchor tags (with many data attributes and other attributes) sometimes prevented the creation of a PDF file, so I wanted to delete them while preserving the text.
Regex:
/ </? a ([^>] *)? > / ig
In PHP you can:
$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'>
Patrick golden
source share