michaelmcgurk
michaelmcgurk

Reputation: 6509

Remove <a> tag from HTML

I'd like to remove the tag from my HTML.

I have the following HTML: <h1><a href="http://www.google.com/">Google</a>& Yahoo</h1>

I'd like to use PHP to convert the above to: <h1>& Yahoo</h1>

Can someone explain how I achieve this?

Upvotes: 1

Views: 103

Answers (1)

Robin
Robin

Reputation: 8498

You can do this with strip_tags().

strip_tags('<h1><a href="http://www.google.com/">Google</a>& Yahoo</h1>', '<h1>');

The result will be <h1>Google & Yahoo</h1>. In case you really want the result <h1>& Yahoo</h1>, you can do it in this way:

preg_replace("@<a[^>]*?>.*?</a>@si", '', '<h1><a href="http://www.google.com/">Google</a>& Yahoo</h1>');

Upvotes: 3

Related Questions