views:

48

answers:

2
..(content).............
<A HREF="http://test.com/content" >test link </A>
...(continue content)...

I want to delete link with content. And also text between link.

A: 

While regular expressions can be used to do this, they will be prone to problems. A more robust solution is using the DOM extension or another HTML parser to remove the a element in question. Or all a elements, for that matter. If you really want to do this with a regular expression, the following should work:

preg_replace('/<A (.*?)>(.*?)</A>/i', '', $data);
You
As you note, this regex will be problematic and will break (e.g. on `<ADDR>`, or even `<A HREF="">...</a>`)
Piskvor
Fixed the regex, it won't break as easily now. While it is a suboptimal solution (which I point out in the answer), it gets the job done, so I don't really see the need for a downvote.
You
+3  A: 

I wouldn't use regex here at all - rather DOMDocument::loadHTML, then DOMDocument::getElementsByTagName and DOMNode::removeChild; finally DOMDocument::saveHTML

Piskvor