preg_replace_callback() with create_function() and awkward strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian.H

    #1

    preg_replace_callback() with create_function() and awkward strings

    Hi all,

    I'm trying to write a PHP colour parser and after various attempts,
    something, somewhere trips it over. Some details:


    String to parse:

    $origString = '^1foo^5bar';


    Required output:

    <span style="color:#F 00; background-color:transpare nt;">foo</span>
    <span style="color:#F 0F; background-color:transpare nt;">bar</span>


    I have an array with all the HTML colour codes:

    $cssColours = array(
    '#000', '#F00', '#3F0', '#FF0',
    '#00F', '#F0F', '#0FF', '#FFF', '#0E0'
    );


    One method I tried was:

    $length = strlen($string) ;
    for ($i = 0; $i < $length; $i++) {
    if ($string[$i] == '^') {
    if (is_numeric($st ring[$i + 1])) {
    if (intval($string[$i + 1]) < 9) {
    if (!$colour) {
    $string = str_replace(
    $string[$i] . $string[$i + 1],
    '<span style="color:' . $cssColours[$string[$i + 1]] .
    '; background-color:transpare nt;">',
    $string
    );
    $colour = true;
    } else {
    $string = str_replace(
    $string[$i] . $string[$i + 1],
    '</span><span style="color:' . $cssColours[$string[$i + 1]] .
    '; background-color:transpare nt;">',
    $string
    );
    }
    }
    }
    }
    }


    This "sort of" works although if the original string contains '^0' as a
    colour code, no closing </span> tag is added (seems to work for all
    other colours) but the other downside to this method is it's slow.

    I then had a look at some regex that I thought might work. The code I
    have for this is:


    $string = stripslashes(su bstr(preg_repla ce_callback(
    '`(?:\^([0-8]))`',
    create_function (
    '$matches',
    '$cssColours = array("#000", "#F00", "#3F0", "#FF0", "#00F", "#F0F",
    "#0FF", "#FFF", "#0E0"); return "</span><span style=\"color:" .
    $cssColours[$matches[1]] . ";\">";'
    ),
    quotemeta($stri ng)
    ), 7)) . '</span>';


    I know this is really tacky looking and it fails in some cases. When
    feeding this:


    echo $lfs->parseColourCod es('^0[^1TST^0]^1M^^0ike');


    it produces (formatted for Usenet):


    <span style="color:#0 00;">[</span>
    <span style="color:#F 00;">TST</span>
    <span style="color:#0 00;">]</span>
    <span style="color:#F 00;">M^</span>
    <span style="color:#0 00;">ike</span>


    which is what I was expecting it to output. However, feeding it:


    echo $lfs->parseColourCod es('^^^[^TST]M^^ike');


    for example gives me:


    ^TST]M^^ike</span>


    which is obviously not what I'm after =)

    Someone did send me a solution they wrote but there's a whole class for
    it alone (the code I'm writing is just a function within a class) and
    was wondering if anyone had any ideas on this that might be solved with
    a "simple" regex?

    The legal colour codes that can be parsed are:


    ^0 - ^8


    but ^ chars are perfectly ok just as carets(^boo^ etc).

    The part I'm not understanding atm, is why the beginning of the string
    is disappearing but it seems to be some kind of "pattern". For example,
    feeding it this:


    echo $lfs->parseColourCod es('^[^TST]M^^ike');


    gives me:


    T]M^^ike</span>


    I've tried with and without the quotemeta() call (there can be pretty
    much any symbolic char, so wasn't sure what might screw over the regex
    pattern) but it's just not happening for me.

    If anyone has any ideas / thoughts / suggestions, I'd be grateful in
    hearing them as right now, I'm pretty stumped =)


    TIA.



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK

  • John Dunlop

    #2
    Re: preg_replace_ca llback() with create_function () and awkward strings

    Ian.H wrote:
    [color=blue]
    > String to parse:
    >
    > $origString = '^1foo^5bar';
    >
    >
    > Required output:
    >
    > <span style="color:#F 00; background-color:transpare nt;">foo</span>
    > <span style="color:#F 0F; background-color:transpare nt;">bar</span>[/color]

    [ ... ]

    $string = preg_replace_ca llback(
    '`\^([0-8])(.*?)(?=(?:\^[0-8])|\z)`',
    create_function (
    '$matches',
    '$cssColours = array("#000", "#F00", "#3F0", "#FF0",
    "#00F", "#F0F", "#0FF", "#FFF", "#0E0");
    return "<span
    style=\"color:{ $cssColours[$matches[1]]};\"[color=blue]
    >{$matches[2]}</span>";'),[/color]
    $string);

    See ciwas for arguments against inline styles.

    HAGW!

    --
    Jock

    Comment

    • Ian.H

      #3
      Re: preg_replace_ca llback() with create_function () and awkward strings

      On Fri, 15 Oct 2004 11:56:26 +0100, John Dunlop
      <usenet+2004@jo hn.dunlop.name> wrote:
      [color=blue]
      >Ian.H wrote:
      >[color=green]
      >> String to parse:
      >>
      >> $origString = '^1foo^5bar';
      >>
      >>
      >> Required output:
      >>
      >> <span style="color:#F 00; background-color:transpare nt;">foo</span>
      >> <span style="color:#F 0F; background-color:transpare nt;">bar</span>[/color]
      >
      >[ ... ]
      >
      >$string = preg_replace_ca llback(
      > '`\^([0-8])(.*?)(?=(?:\^[0-8])|\z)`',
      > create_function (
      > '$matches',
      > '$cssColours = array("#000", "#F00", "#3F0", "#FF0",
      > "#00F", "#F0F", "#0FF", "#FFF", "#0E0");
      > return "<span
      > style=\"color:{ $cssColours[$matches[1]]};\"[color=green]
      > >{$matches[2]}</span>";'),[/color]
      > $string);
      >
      >See ciwas for arguments against inline styles.
      >
      >HAGW![/color]


      Ahh John you're a diamond!

      The only reason I opted for the create_function () call was because it
      failed every time I tried to use either:


      $this->someFunction ()

      or

      FOO::someFuncti on()


      I've never actually used this function before.. it came as a result of
      using an array in preg_replace():


      $string = preg_replace('/pattern/', "{$cssCodes[$1]}", $string);


      and all other variations I could think of failing.

      If there's a better way, I'm all for hearing! I didn't think this was
      anywhere near a "perfect" solution.. just a possible solution =)

      Thanks again for your answer.. I can now retain some hairs =D



      Regards & good weekend to yourself...

      Ian

      --
      Ian.H
      digiServ Network
      London, UK

      Comment

      • Ian.H

        #4
        Re: preg_replace_ca llback() with create_function () and awkward strings

        On Fri, 15 Oct 2004 11:58:35 GMT, Ian.H <ian@WINDOZEdig iserv.net> wrote:


        [ snip ]
        [color=blue][color=green]
        >>See ciwas for arguments against inline styles.
        >>
        >>HAGW![/color]
        >[/color]
        [color=blue]
        >
        >The only reason I opted for the create_function () call was because it
        >failed every time I tried to use either:[/color]

        [ ... SNIP ... ]


        Oops! I really misinterpreted this heh (it was late, very).

        I don't use inline styles under normal circumstances, but this class is
        being written for a "gaming community".. so I don't know what styles
        they may / may not already have or want. This is also pretty much the
        first release of this code and have thought about moving certain things
        into a CSS file but don't know really how practical it'd be for a couple
        of lines for this. When coding a site however, I never use inline
        styles.. CSS all the way (along with XHTML) and validated naturally =)



        Regards,

        Ian

        --
        Ian.H
        digiServ Network
        London, UK

        Comment

        Working...