Re: preg_replace: removing IMG-tags from string?

From: d (d_at_example.com)
Date: 12/03/04


Date: Fri, 03 Dec 2004 10:46:15 GMT


The "/"s are delimiters, saying "everything between these slashes is a
regular expression". The regular expression (or pattern) is quite simple:

'<img' - is straight text, so it will try and match this
'.*' - is more complicated. The '.' means 'any character', and the '*'
means 'one or more', so together they mean 'one or more of any character'
'>' - again, is straight text.

The two characters after the last slash are called "pattern modifiers", and
tell PHP how to treat your pattern. Ususally, you don't need them, but for
slightly more complicated patterns, they're very useful. The U means
"ungreedy", which tells PHP to only match enough to match the pattern, no
more. If I took that out, it would match from <img up to the last '>'
character in the entire string. With the 'U', it matches up to the first
'>', which is what we want. The 'i' makes the pattern case-insensitive, so
'<img' matches '<IMG' or '<Img', or any other combo.

Let me know if you have any unanswered questions :)

d

So, together, it means "any string that starts with <img and ends in >"

"Harrie Verveer" <newsgroup{remove-this}@harrieverveer.com> wrote in message
news:vcOdnQm1MZJu3S3cRVnygA@zeelandnet.nl...
> Thanks d, that works perfectly for me :) However, I'd like to understand
> what it says - I almost get the whole regular expression. Please tell me
> if I'm right:
>
> <img.*> "<img", with everything behind it (.*), ending with ">"
>
> /<something>/
> take the value between / and / as the value that should be replaced
>
> Ui
> ??? regardless of case?
>
>
>
> d wrote:
>> $fixed = preg_replace("/<img.*>/Ui", "", $string);
>>
>> That says, replace any string starting with "<img" up to the first ">"
>> character, regardless of case.
>>
>> It's not perfect, but will suffice in most situations ;)



Relevant Pages

  • Re: Emoticon text parser
    ... That is very close to a regular expression already. ... Most of these are character sets. ... We can ease the pain with the COMMENT flag of Pattern; ... The pattern really just addresses strings that are *exactly* 2-7 chars ...
    (comp.lang.java.programmer)
  • Re: RegEx: How to ignore the number of whitespaces?
    ... a "simpler" regular expression syntax is likely to bite you eventually, ... but that some of these character sequences may be "marked" as ... This is a regular expression "if" conditional statement, ... do not understand why the pattern "personal computer" will only match ...
    (microsoft.public.dotnet.framework)
  • Re: Expert script (.bat) writers help needed (strip double-quote from string)
    ... Sets or returns the regular expression pattern being searched for. ... Always a RegExp object variable. ... May include any of the regular expression characters defined in the table in the Settings section. ...
    (microsoft.public.windowsxp.help_and_support)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)