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 11:53

On Wed, 23 Aug 2006 11:25:17 +0200, "Rik" <luiheidsgoeroe@hotmail.com>
wrote:

>Mike Collins wrote:
>> if (preg_match('/<\s*img/i',
>> $row['text'])){ $row['text'] = preg_replace(
>> '/<img\s*[^>]*alt=[\'"]([^\'"]*?)[\'"][^>]+>/iu', '$1',
>> trim($row['text']) ) . ' (<b>IMG ALT</b>)';
>> }
>>
>> regex isolated:
>> ----------------------------
>> /<img\s*[^>]*alt=[\'"]([^\'"]*?)[\'"][^>]+>/iu
>> ----------------------------
>-------------------------------------------^---^
>
>There must be room after the closing tag, and having \s* and [^>]* together
>is useless
>
>
>> supposed to do:
>> - strip alt attribute from img tag
>>
>> almost works:
>> - fails on empty alt tag (e.g. alt="")
>
>/<img[^>]*?alt=[\'"]([^\'"]*)[\'"][^>]*>/si

Regex now works. It was failing because I mistakenly thought I was
extracting the alt text from an img tag. The target string was
actually the text between a tags.

snoopy.class.php
htmlsql.class.php

This started playing around with the examples in htmlsql.class.php

If you know what snoopy is you might want to play with htmlsql too.
The htmlsql examples that return links were displaying broken image
tags if the a displayed an image instead of text. It turned out to be
a nice exercise, making the broken image tag output pretty by
displaying the alt text of the image.

Shown below is modified output from demo02.php
htmlsql.class.php
http://www.jonasjohn.de/lab/htmlsql.htm

// show results:
$totalRows = count($wsql->fetch_array());
foreach($wsql->fetch_array() as $row){
echo $totalRows . '<br>' .
'HREF: ' . trim($row['url']) . "<br>\n";

if (
preg_match('/<\s*img/i', $row['text'])
)
{
$row['text'] =
preg_replace(
'/<img[^>]*?(alt\s*=\s*[\'"][^\'"]*[\'"])[^>]+>.*/i',
'$1', $row['text']) . ' (<b>IMG ALT</b>)';
}
echo 'TEXT: ' . trim($row['text']);
echo "<br><br>\n";
$totalRows--;
}

// just a few more comments on the regex
-- the original regex was not operating on the whole string

I do believe the regex looks pretty good. It accounts for single and
double quotes and spaces around the equals. Any other observations?

 

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

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