查找链接并从HTML中删除它们

How can I look for links in HTML and remove them?

$html = '<p><a href="javascript:doThis('Test Title 1')">Test Title 1</a></p>';
$html .= '<p><a href="javascript:doThis('Test Title 2')">Test Title 2</a></p>';
$html .= '<p><a href="javascript:doThis('Test Title 3')">Test Title 3</a></p>';

$match = '<a href="javascript:doThis('Test Title 2')">';

I want to remove the anchor but display the text. see below.

Test Title 1

Test Title 2

Test Title 3

I've never used Regular Expressions before, but maybe i can avoid it also. Let me know if im not clear.

Thanks

Mark

EDIT: its not a client side thing. I cant use javascript for this. I have a custom CMS and want to edit HTML stored in a Database.

You could see if Simple HTML DOM does the trick.

You can use

var foo = document.getElementsByTagName('a');

to fetch all the link tags. No need for regular expressions here...

EDIT: I'm just learning to read... ;) Go with PHP's DOM or XML abilities. It should be pretty easy using those.

You may try the simplest thing:

echo strip_tags($html, '<p>');

This strips all tags except <p>

If you really like regexp:

echo preg_replace('=</?a(\s[^>]*)?>=ims', '', $html);

EDIT:

Delete a - tag AND surrounding tags (code gets messy and doesn't work with broken (X)HTML):

echo preg_replace('=<([a-z]+)[^>]*>\s*<a(\s[^>]*)?>(.*?)</a>\s*</\\1>=ims', '$3', $html);

Howerwer if your problem is that complicated, I recommend that you try xpath.

You might have some joy with Beautiful Soup - http://www.crummy.com/software/BeautifulSoup/ (Python HTML parsing / manipulation API)

sed -i -e 's/<a.*<\/a>//g' filename.html

Note that using regular expressions for hacking HTML is a... dubious proposition, but it might just work in practice ;-)

open the HTML file in Microsoft Expression. Ctrl+F and then chose replace tag or tag attributes contents Easy and quick solution Thanks Shomaail