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"

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

This isn't a good idea. The pony he comes
@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.
What is the practical difference, though? You're still parsing it with regex, which doesn't work well with nested languages like HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.