You are here: Re: regex fails stripping alt from img tag « All PHP « IT news, forums, messages
Re: regex fails stripping alt from img tag

Posted by Mike Collins on 08/23/06 16:53

On Wed, 23 Aug 2006 16:33:35 +0200, "Rik" <luiheidsgoeroe@hotmail.com>
wrote:

>Mike Collins wrote:
>> '/<img[^>]*?(alt\s*=\s*[\'"][^\'"]*[\'"])[^>]+>.*/i',
>> '$1', $row['text']) . ' (<b>IMG ALT</b>)';
>> }
>> I do believe the regex looks pretty good. It accounts for single and
>> double quotes and spaces around the equals. Any other observations?
>
>
>I'm curious what the reason for the last .* is. You want to replace
>everything up to the end of the line?
>Also, once it's matched either a single or double quote, best to stick with
>it offcourse, so alt='15" monitor' works:
>
>'/<img #opening tag
> [^>]*? #random chars within tag up until next match
> (alt\s*=\s* #start of alt attribute
> (\'|") #get the quote
> ([^\2]*) #everything except the used quote
> \2) #endquote, end of alt-attribute
> [^>]* #allow for excess chars within the tag (why even
>match them?)
>>.*/ix',
>
>Grtz,

Very excellent, thank you. You'd think it would be simple to pull the
alt attribute out of a tag. I suppose in the overall scheme of things
it's not that great of an acccomplishment but it did require some
thought.

preg_replace(
'/<img #comments are cool
[^>]*?
(
alt\s*=\s*
(\'|") #match the quote
([^\2]*?) #get alt content; dont be greedy
\2 #use matched quote
)
..* #chop and toss
/ix
',
'$1', $row['text']) . ' (<b>IMG ALT</b>)';

The tag below won't ruin my day but the results won't be pretty. Tried
the m modifier, no dice.

<img src="/img/l_eng.png"
alt=""
width="27"
height="20"
/>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация