eregi to get the alt tag in a string

Discussion in 'PHP' started by sarahk, Oct 30, 2005.

  1. #1
    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];
    }
    PHP:
    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" />
    Code (markup):
    returns
    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
    Code (markup):
    when it should return
    Fort Knox in a Box. Visit Lenovo to learn more.
    Code (markup):
    Any ideas on how to get the regular expression to behave?
     
    sarahk, Oct 30, 2005 IP
  2. frankm

    frankm Active Member

    Messages:
    915
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    83
    #2
    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];
    }
     
    frankm, Oct 30, 2005 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,840
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Perfect, thank you very much Frank!
     
    sarahk, Oct 30, 2005 IP
  4. frankm

    frankm Active Member

    Messages:
    915
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    83
    #4
    :) no prob. regular expressions are great if they work out ... but most of the time they don't :)
     
    frankm, Oct 30, 2005 IP