madphp
madphp

Reputation: 1764

Find Links and Remove them from 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.

Upvotes: 1

Views: 1706

Answers (6)

Justin
Justin

Reputation: 86799

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

Upvotes: 1

Shomaail
Shomaail

Reputation: 1

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

Upvotes: 0

hegemon
hegemon

Reputation: 6764

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.

Upvotes: 4

Yacoby
Yacoby

Reputation: 55465

You could see if Simple HTML DOM does the trick.

Upvotes: 3

Franz
Franz

Reputation: 11553

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.

Upvotes: 0

Jonas K&#246;lker
Jonas K&#246;lker

Reputation: 7837

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 ;-)

Upvotes: 0

Related Questions