Re: preg_replace: removing IMG-tags from string?
From: d (d_at_example.com)
Date: 12/03/04
- Next message: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Previous message: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- In reply to: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Next in thread: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Reply: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Reply: Jonathan Lozinski: "Re: preg_replace: removing IMG-tags from string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 ;)
- Next message: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Previous message: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- In reply to: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Next in thread: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Reply: Harrie Verveer: "Re: preg_replace: removing IMG-tags from string?"
- Reply: Jonathan Lozinski: "Re: preg_replace: removing IMG-tags from string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|