Web Analytics Made Easy -
StatCounter Converting characters to font colors. - CodingForum

Announcement

Collapse
No announcement yet.

Converting characters to font colors.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    if ( bad && possible ) happen();
    Sig re-written for faster processing...

  • #2
    Hi,

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

    Comment


    • #3
      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>";
      }
      ?>
      if ( bad && possible ) happen();
      Sig re-written for faster processing...

      Comment


      • #4
        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)

        Comment


        • #5
          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
          if ( bad && possible ) happen();
          Sig re-written for faster processing...

          Comment


          • #6
            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?

            Comment


            • #7
              Yeah, that's the current problem. It don't wanna work.
              if ( bad && possible ) happen();
              Sig re-written for faster processing...

              Comment


              • #8
                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; Jul 9, 2002, 11:34 AM.
                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)

                Comment


                • #9
                  Thanks a bunch. That's a lot farther than I'd been able to get.
                  if ( bad && possible ) happen();
                  Sig re-written for faster processing...

                  Comment


                  • #10
                    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...

                    Comment

                    Working...
                    X