View Full Version : Converting characters to font colors.

Jul 7th, 2002, 09:12 PM

I have a simple script to parse a text file and display it in the browser. My next project has been to convert the color codes my server (MUD) uses into font colors. Any tips to do this?

This is what one line of the text dump may look like:
NSTR Siara: (long) {xA {Ws{wi{Dlve{wr{Wy {xbladed shortsword with a {Cc{cr{wy{Cs{wt{ca{Cl {xhilt{x belongs to Siara.

I'm thinking I need to parse each line for the { then look at { +1 and convert the { to # and the following letter to a pre-defined number-letter font color code before it gets echo'd, but that's where I'm stuck.

Something like?

x = strlen($data);
for( i = 0; i <= x; i++) {
if ( i == '{' && i+1 == 'W' ) $code = '#FFFFFF';

I've also looked at substr_replace, but not sure about using it with variable instances being replaced within the string.

Hope I'm making sense, and thanks for any tips.

Eric, aka Dalsor

Jul 8th, 2002, 12:44 PM

What colour codes map onto what font colours...?
Are you trying to change the server dump to some sort of HTML output?

Jul 8th, 2002, 10:02 PM
Trying to convert them when the PHP script parses the text document.

This is a switch from a piece of C code that I use that lists each of the codes and their correspoding font codes, the difference being, this code converts the color codes prior to the HTML being written, whereas I'm trying to write a PHP script to convert the color codes to font codes from a file that already exists.

case 'x':
sprintf( code, "<font color=""#006400"">" );
case 'b':
sprintf( code, "<font color=""#00008B"">" );
case 'c':
sprintf( code, "<font color=""#008B8B"">" );
case 'g':
sprintf( code, "<font color=""#006400"">" );
case 'm':
sprintf( code, "<font color=""#8B008B"">" );
case 'r':
sprintf( code, "<font color=""#8B0000"">" );
case 'w':
sprintf( code, "<font color=""#808080"">" );
case 'y':
sprintf( code, "<font color=""#808000"">" );
case 'B':
sprintf( code, "<font color=""#0000FF"">" );
case 'C':
sprintf( code, "<font color=""#OOFFFF"">" );
case 'G':
sprintf( code, "<font color=""#00FF00"">" );
case 'M':
sprintf( code, "<font color=""#FF00FF"">" );
case 'R':
sprintf( code, "<font color=""#FF0000"">" );
case 'W':
sprintf( code, "<font color=""#FFFFFF"">" );
case 'Y':
sprintf( code, "<font color=""#FFFF00"">" );
case 'D':
sprintf( code, "<font color=""#636363"">" );
case '{':
sprintf( code, "{" );

This is the PHP script that reads and echoes the flat file sans my futile attempts to convert the strings. :o

$strings = file("/path/to/my/dir/nstring.dat");
foreach($strings as $line){
// before echo, loop through $line and convert codes
echo $line . "<br>";

Jul 9th, 2002, 05:39 AM
lol - I still have not a clue whats going on here...

$col=array(x=>"<font color=\"#006400\">",b=>"<font color=\"#00008B\">",c=>"<font color=\"#008B8B\">",
g=>"<font color=\"#006400\">",m=>"<font color=\"#8B008B\">",r=>"<font color=\"#8B0000\">",w=>"<font color=\"#808080\">",
y=>"<font color=\"#808000\">",B=>"<font color=\"#0000FF\">",C=>"<font color=\"#OOFFFF\">",G=>"<font color=\"#00FF00\">",
M=>"<font color=\"#FF00FF\">",R=>"<font color=\"#FF0000\">",W=>"<font color=\"#FFFFFF\">",Y=>"<font color=\"#FFFF00\">",
D=>"<font color=\"#636363\">");

$str="NSTR Siara: (long) {xA {Ws{wi{Dlve{wr{Wy {xbladed shortsword with a {Cc{cr{wy{Cs{wt{ca{Cl {xhilt{x belongs to Siara.";
echo htmlspecialchars($str);
echo "<br /><br />";
eval("echo \"$str\";");

does some interesting stuff, but I am unsure where and when you want the font colours to appear and around what??
is '{Ws'
supposed to translate to
{<font colour='#whatever'>s</font> ??

anyway the above may help?

it actually begs another question,

I a unsure how to achieve this within the ereg statement (which is why I eval'led)


'$col[\\1]' throws an error but there must be a way of achieving this??

Jul 9th, 2002, 06:24 AM
is '{Ws'
supposed to translate to
{<font colour='#whatever'>s</font> ??

Exactly! :) I probably should have said that in the first place. Bad habit I have, trying to over-complicate things.

This is what it is currently producing: http://aww-mud.org/nstring.php from this dump: http://www.aww-mud.org/nstring.dat

Jul 9th, 2002, 06:30 AM
:( I uh tried your page... this is what I got....

(I used code just for reference, it s not really code ;)

NSTR Fidias: (short) </font>{".$col[]."A </font>{".$col[]."S</font>{".$col[]."e</font>{".$col[]."n</font>{".$col[]."s</font>{".$col[]."e </font>{".$col[]."of </font>{".$col[]."D</font>{".$col[]."u</font>{".$col[]."t</font>{".$col[]."y</font>{".$col[]."

Fatal error: Cannot use [] for reading in /home/aww/public_html/nstring.php(17) : eval()'d code on line 1

Is it just me, or does anyone else get somehting like that?

Jul 9th, 2002, 06:37 AM
Yeah, that's the current problem. :) It don't wanna work.

Jul 9th, 2002, 05:30 PM
this => http://www.firepages.org/stuff/ereg.php throws a parse error on one of the lines, but the rest appear to work??

x=>"<font color=\"#006400\">",b=>"<font color=\"#00008B\">",
c=>"<font color=\"#008B8B\">",g=>"<font color=\"#006400\">",
m=>"<font color=\"#8B008B\">",r=>"<font color=\"#8B0000\">",
w=>"<font color=\"#808080\">",y=>"<font color=\"#808000\">",
B=>"<font color=\"#0000FF\">",C=>"<font color=\"#OOFFFF\">",
G=>"<font color=\"#00FF00\">",M=>"<font color=\"#FF00FF\">",
R=>"<font color=\"#FF0000\">",W=>"<font color=\"#FFFFFF\">",
Y=>"<font color=\"#FFFF00\">",D=>"<font color=\"#636363\">");

foreach($yaks as $str){
eval("echo \"<font color=navy>$str\";");
echo "<br />";

lol just noticed that both the forum and the highlight_string() function strip a '\' from the code

$col[\1] should read $col[\\1]

Jul 9th, 2002, 07:17 PM
Thanks a bunch. That's a lot farther than I'd been able to get.

Jul 10th, 2002, 07:06 AM
I would also make sure that you set them so that the will work with a gackground color... Black would probably be the best choice of one...