Need help with PHP regular expressions and eregi_replace

Discussion in 'OT Technology' started by TheDarkHorizon, Aug 12, 2004.

  1. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    I set up a string
    $str = "(FONT=Arial)My Message(/FONT)(FONT=Verdana)Another Message(/FONT)";
    and for my regular expression, I used:
    $regex = "\(FONT=(.*)\)(.*)\(/FONT\).*";
    and used this for the replacement:
    $replacement = "<span style=\"font-family: \\1\">\\2</span>";
    But when I use multiple tags, it will only replace it once. How can I make it replace all the tags globally? I tried a ghetto loop which counted how many FONT tags were used, and then kept appending the same string to $regex and $replacement (replacing the \\1 and \\2), but it will only go to \\9, limiting me to 4 FONT tags max before it starts messing up.

    I'm sure there is an easier way to do this than what I am trying to do. Basically, I want all (FONT=font)Message(/FONT) to look like <span style="font-family: font">Message</span>
     
  2. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    I've worked around it by using both eregi_replace and str_replace, but I feel it is a bit dirty.

    My regex string is now:
    $regex = "\(FONT=([ a-zA-Z0-9]*)\)";
    And my replacement is now:
    $replacement = "<span style=\"font-family: \\1\">";
    At the end, I do:
    str_replace("(/FONT)", "</span>", $str);
    My concerns with this method are,
    1. (/FONT) must be uppercase.
    2. Regular expression stops when it does not reach an alpha-numeric [ a-zA-Z0-9] character, so I am assuming that all fonts will only contain an alphabet, a number, or a space. Is that bad to assume?
     
  3. maczter

    maczter Life is trying things to see if they work.

    Joined:
    Sep 30, 2003
    Messages:
    3,622
    Likes Received:
    0
    Location:
    Austin, TX
    nice av.

    orange is the new black? :)
     
  4. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Orange rocks. I've been using my shitty workaround I posted, and it seems to be working fine so far. But if you guys find anything foolish in it, tell me. Thanks. :)
     

Share This Page