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:#F00; background-color:transparent;">foo</span>
<span style="color:#F0F; background-color:transparent;">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($string[$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:transparent;">',
$string
);
$colour = true;
} else {
$string = str_replace(
$string[$i] . $string[$i + 1],
'</span><span style="color:' . $cssColours[$string[$i + 1]] .
'; background-color:transparent;">',
$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(substr(preg_replace_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($string)
), 7)) . '</span>';
I know this is really tacky looking and it fails in some cases. When
feeding this:
echo $lfs->parseColourCodes('^0[^1TST^0]^1M^^0ike');
it produces (formatted for Usenet):
<span style="color:#000;">[</span>
<span style="color:#F00;">TST</span>
<span style="color:#000;">]</span>
<span style="color:#F00;">M^</span>
<span style="color:#000;">ike</span>
which is what I was expecting it to output. However, feeding it:
echo $lfs->parseColourCodes('^^^[^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->parseColourCodes('^[^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
http://digiserv.net/