Matt Ryan
Matt Ryan

Reputation: 87

How to remove HTML tags from strings?

I am looking to remove a chunk out of a string dynamically, from the occurrence of one character < to the occurrence of another set of 2 characters />. Here's an example of what might be output:

<img src='blah.png' width="300" height="225" />There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slight...

It might not be an img tag, it could be an img inside an anchor tag, or no tags at all. What I'm after is the string after the tags (if any), more specifically the first 50 words. But I have the stripping down to 50 words thing working, i just need the tags ripped out. And I can't cheat and use img {display:none;} as the characters are counted.

Is this possible using PHP?

Upvotes: 0

Views: 167

Answers (1)

Roberto Maldonado
Roberto Maldonado

Reputation: 1595

How about:

$string = "<img src='blah.png' width="300" height="225" />There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slight...";
$new_string = preg_replace('/<.*?\/>/','',$string);

Still, if you're just parsing html tags, may regexp wouldn't be the best way to go. But if it's just removing some strings, then it may help.

Upvotes: 2

Related Questions