Hi Maxicube - that's some interesting code on trackmania. Haven't seen that type of encoding before.
Can you give more details about how this is going to be used?
For the DB example:
The only thing I can think of is chopping up the string into bits using explode on the '$' character. You know your last chunk is the yellow, penultimate is black and the previous one is yellow. Any bits left over can be compared with a simple array to get the right format (e.g. $i%s -> <em>%s</em>).
If Just colours:
Each example uses the $ + 3 hexadecimal chars. You can use the FONT tag and add a 'color' attribute - but this is bad. A span tag with the 'color' attribute would be more acceptable, although a 'class' attribute linked to a CSS file would be really good. How you'd do the latter I'm not sure as each color would probably require its own class name - bit awkward.
So:
$f00MAXICUBE turns to
<span color="#ff0000">MAXICUBE</span>
Basically you need to double each hex character and relpace the $ with a #, surround the text part with span tags and insert a color attribute.
Personally, I'd write a function something like this (it's a bit dirty - all the substr!):
function colorize($tmString){
$colorhex = '#' . substr($tmString,1,1) . substr($tmString,1,1) . substr($tmString,2,1) . substr($tmString,2,1) . substr($tmString,3,1) . substr($tmString,3,1);
$string = substr($tmString,4);
$output = "<span color=\"{$colorhex}\">{$string}</span>";
return $output;
}
USAGE:
$txt = "$f00MAXICUBE";
echo "I went to a bar and saw " . colorize($txt) . " drinking a soda...";
Don't know if that's any use?