Forum Moderators: coopster

Message Too Old, No Replies

Regex/preg_replace help

         

Norky

7:29 pm on Apr 4, 2005 (gmt 0)

10+ Year Member



I've never grasped regex, and I doubt I ever will.

All I simply need is a preg_replace expression for removing all <img> tags from a string.

What I have at the moment is:

@<img[^>]>@si

From what I understand, that should simply match...

<img **ANYTHING-EXCEPT-'>'** >

..but it does not seem to work. Where am I going wrong?

gliff

8:32 pm on Apr 4, 2005 (gmt 0)

10+ Year Member



The following

[^>]

will indeed match any character that is not a ">". However, it will only match one occurrence of this character. The following strings would be matched by your regular expression.


<img >
<imga>
<img4>
<img#>

What you want is something like this


@<img[^>]+>@si

The "+" indicates "match the preceding character/character class one or more times". With that in place, you should be able to catch strings like


<img src="foo" height="blah" />

killroy

9:55 pm on Apr 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or us [^>]* for zero or more times, perhaps more appropriate as you might want to match stuff like <br> for example.

SN