PHP regular expression to remove tags in HTML document


Question

Say I have the following text

..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...

I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the URLs will all be different)

Much thanks

1
6
9/6/2009 6:42:18 AM

Accepted Answer

Avoid regular expressions whenever you can, especially when processing xml. In this case you can use strip_tags() or simplexml, depending on your string.

16
5/23/2017 11:45:36 AM

This will remove all tags:

preg_replace("/<.*?>/", "", $string);

This will remove just the <a> tags:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon