Myspace Layouts - Debt Consolidation - Debt Consolidation - Wordpress Themes - Debt Consolidation

PDA

View Full Version : eregi to get the alt tag in a string


sarahk
Oct 30th 2005, 6:22 pm
I'm writing a wee link extractor and it works fine for text links but I need it to handle links behind images.

I've got this code if (eregi( ".*<IMG.*ALT=\"(.*)\".*>.*", $link, $out)) {
$curLink['title'] = $out[1];
}
but it picks up everthing from the alt tag to the last "

for example this image
<img alt="Fort Knox in a Box. Visit Lenovo to learn more." src="/us/images/2005/10/102505_T7_Lenovo_Vault.gif" width="152" height="60" border="0" />returnsFort Knox in a Box. Visit Lenovo to learn more." src="/us/images/2005/10/102505_T7_Lenovo_Vault.gif" width="152" height="60" border="0when it should returnFort Knox in a Box. Visit Lenovo to learn more.

Any ideas on how to get the regular expression to behave?

frankm
Oct 30th 2005, 6:33 pm
a " sign cannot exist in a ALT="" tag, should be a &quot; sign, so
changing your regexp (.*) to ([^\"]*) will probably fix it.

if (eregi( ".*<IMG.*ALT=\"([^\"]*)\".*>.*", $link, $out)) {
$curLink['title'] = $out[1];
}

sarahk
Oct 30th 2005, 6:38 pm
Perfect, thank you very much Frank!

frankm
Oct 30th 2005, 6:50 pm
:) no prob. regular expressions are great if they work out ... but most of the time they don't :)