Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Florida, USA
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting characters to font colors.

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

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

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Florida, USA
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

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

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,093
    Thanks
    11
    Thanked 101 Times in 99 Posts
    lol - I still have not a clue whats going on here...

    PHP Code:
    <?
    $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??
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Jul 2002
    Location
    Florida, USA
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I uh tried your page... this is what I got....

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

    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?

  • #7
    New Coder
    Join Date
    Jul 2002
    Location
    Florida, USA
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, that's the current problem. It don't wanna work.

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,093
    Thanks
    11
    Thanked 101 Times in 99 Posts
    this => http://www.firepages.org/stuff/ereg.php throws a parse error on one of the lines, but the rest appear to work??

    PHP Code:
    <?
    $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]
    Last edited by firepages; 07-09-2002 at 04:34 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    New Coder
    Join Date
    Jul 2002
    Location
    Florida, USA
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a bunch. That's a lot farther than I'd been able to get.

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •