Help with Regex

Discussion in 'PHP' started by Strike X, Apr 14, 2009.

  1. #1
    There is some tags with missing quote.

    <img src=file.jpg border="0" alt=text goes here title=this is a title />

    replace to:

    <img src="file.jpg" border="0" alt="text goes here" title="this is a title" />

    How can that be done?
     
    Strike X, Apr 14, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $str = preg_replace('`([a-z])=([^"]*?)\s+(?=[a-z]+=|/)`', '\1="\2" ', $str);
    Code (markup):
    Note that it's a fragile regex due to the fundamentally ambiguous nature of your inputs. Things like slashes and equal signs in the attribute values can mess it up.
     
    SmallPotatoes, Apr 14, 2009 IP