...

View Full Version : Converting characters to font colors.



Dalsor
07-07-2002, 08:12 PM
Greetings,

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

Mouldy_Goat
07-08-2002, 11:44 AM
Hi,

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

Dalsor
07-08-2002, 09: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"">" );
break;
case 'b':
sprintf( code, "<font color=""#00008B"">" );
break;
case 'c':
sprintf( code, "<font color=""#008B8B"">" );
break;
case 'g':
sprintf( code, "<font color=""#006400"">" );
break;
case 'm':
sprintf( code, "<font color=""#8B008B"">" );
break;
case 'r':
sprintf( code, "<font color=""#8B0000"">" );
break;
case 'w':
sprintf( code, "<font color=""#808080"">" );
break;
case 'y':
sprintf( code, "<font color=""#808000"">" );
break;
case 'B':
sprintf( code, "<font color=""#0000FF"">" );
break;
case 'C':
sprintf( code, "<font color=""#OOFFFF"">" );
break;
case 'G':
sprintf( code, "<font color=""#00FF00"">" );
break;
case 'M':
sprintf( code, "<font color=""#FF00FF"">" );
break;
case 'R':
sprintf( code, "<font color=""#FF0000"">" );
break;
case 'W':
sprintf( code, "<font color=""#FFFFFF"">" );
break;
case 'Y':
sprintf( code, "<font color=""#FFFF00"">" );
break;
case 'D':
sprintf( code, "<font color=""#636363"">" );
break;
case '{':
sprintf( code, "{" );
break;

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

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

firepages
07-09-2002, 04: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.";
$str=eregi_replace("\{([a-z]{1})","</font>{\".\$col[\\1].\"",$str);
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??
i.e.
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)

$str=eregi_replace("\{([a-z]{1})","{$col[\\1]",$str);

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

Dalsor
07-09-2002, 05:24 AM
i.e.
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

Cloudski
07-09-2002, 05: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?

Dalsor
07-09-2002, 05:37 AM
Yeah, that's the current problem. :) It don't wanna work.

firepages
07-09-2002, 04: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??



<?
$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\">");

$yaks=file('amazing.txt');
foreach($yaks as $str){
$str=eregi_replace("\{([a-z]{1})","</font>{\".\$col[\\1].\"",$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]

Dalsor
07-09-2002, 06:17 PM
Thanks a bunch. That's a lot farther than I'd been able to get.

Cloudski
07-10-2002, 06: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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum