0

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?

2
  • 8
    Did you try strip_tags? ar2.php.net/manual/en/function.strip-tags.php Commented Sep 9, 2013 at 18:40
  • I was looking at strip_tags but there might be text inside of a tag like an anchor tag, that I don't want in there. Thanx for your input. Commented Sep 9, 2013 at 18:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I was hoping to avoid regexp, but it may be my only solution. Thanx for your input.
Tags don't usually end with /> (some do, sometimes, but you probably can't count on that). @MattRyan Regex is great. Why not use it? It's great!
@Rudie, I just answered what OP asked D:, and yeah, about regex, totally agree
He asked about self closing tags only? I missed that. Still don't see it, but I believe you. #regex4ever!
You are right, tags these days, are not supposed to be self closing anymore(since HTML5). At least i think that's the term. Most CMS' still use them though, as far as I know anyways. I typically use regexp for things like testing email addresses and such, when it comes to HTML tags and a user creating them, regexp is not 100% reliable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.