0

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"
Adam
  • 125
  • 10

1 Answers1

1

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);
Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39
  • This isn't a good idea. [The pony he comes](http://stackoverflow.com/a/1732454/2370483) – Machavity Sep 04 '14 at 15:52
  • @Machavity It doesn't matter since he only wants to replace it with something. He doesn't want to parse HTML to do something later with it. – Charlotte Dunois Sep 04 '14 at 15:59
  • What is the practical difference, though? You're still parsing it with regex, which doesn't work well with nested languages like HTML – Machavity Sep 04 '14 at 16:16