AnJ
AnJ

Reputation: 125

PHP Find and replace certain string pattern

I have piece of html code in php $string . The problem is, I need to remove all "img" and "a href" tags from it. I guess I should use preg_replace function, but how should my patterns look like?

I would like to replace:

"<img some params and address>" with ""

and

"<a href="some random address with unknown length">my text</a>" with "my text"

Upvotes: 0

Views: 75

Answers (1)

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

You can use preg_replace with Regular Expressions. Something like this:

$text = preg_replace("/<img (.*?)>/i", "", $text);
$text = preg_replace("/<a (.*?)>(.*?)</a>/i", "$2", $text);

Upvotes: 1

Related Questions