Page 1 of 1

not PHP programmer needs help

Posted: Fri Oct 20, 2006 8:35 am
by MeByMySelf
Hello,

On my website I have a code block that retrieve and render information about Game Server
The script works Ok except one little detail:
Server name may contain color codes. I need to remove those codes or (which will be great!) replace with real color

the color code is 4 byte lohg 1st byte is escape char (chr(27)) and then coming 3 bytes of RGB

for example chr(27) . chr(255) . chr(1) . chr(1) means RED

I need some function that will accept as parameter a string with color codes and return the same string without any formatting.

so actually it should look for chr(27) and replace it and tree following chars with empty string.

here is sample of input string

Code: Select all

ÿ=TAG=ÿServer name
and here is desired output

Code: Select all

=TAG=Server name
Or if possible this:

Code: Select all

<font color="#FF0101">=TAG=</font><font color="#0101FF">Server name</font>
Please note that 00 doesn't used in color codes so BLACK color isn't "totaly" BLACK but 99% grey '#010101'

I don't know PHP syntax to accomplish this task :(

I appreciate your help

Re: not PHP programmer needs help

Posted: Fri Oct 20, 2006 10:56 am
by RobertGonzalez
MeByMySelf wrote:The script works Ok except one little detail:
Server name may contain color codes. I need to remove those codes or (which will be great!) replace with real color
This confuses me a little bit (ok, a great bit). What does this mean?

Posted: Fri Oct 20, 2006 11:02 am
by MeByMySelf
have you ever played multiplayer online games?

If yes you probably noticed that server's name might have colors.
I am showing server info on the web page but it also shows some garbage instead of colors - that what i want to remove/ replace with html colors

BTW, I am almost finished it by myself using php manual :)

Posted: Fri Oct 20, 2006 11:05 am
by RobertGonzalez
Well, that is a testament to the manual. When you get it worked out, post what you did. I have never gamed so I am still at a loss for what you are talking about. But it sounds like you might have things worked out, so good luck.

Posted: Fri Oct 20, 2006 11:05 am
by MeByMySelf
here you go

Code: Select all

function cleanup($string){
     $done = false;
     $count=0;
     while($done == false){
          $out = unpack("H*", $string);
          $pos = strpos($out[1], "1b");
          $len = "4";
          
          if ($pos === false){
               $done = true;
               break;
          }
          $count++;

          /* Calc true position */
          if($pos == 0){
               $loc = $pos;
          }else{
               $loc = $pos / 2;
          }

         $color=join(unpack("H*", substr($string, $loc+1, $len-1)))   ;
                 
              
         $substr='<' . 'font color="#' . $color . '">' ;
         $string = substr_replace($string, $substr, $loc, $len);


     }
     
while ( $count>0 ){
    $string = $string  . '<'  .'/font'.  '>' ;
    $count-- ;
  }
     return $string;
}

Posted: Fri Oct 20, 2006 11:09 am
by MeByMySelf
you can see how it works on my page

There is "-WoW- Game Server" block on the left side the name of the server colored using this function

Posted: Fri Oct 20, 2006 11:16 am
by RobertGonzalez
Cool glad you got it. And thanks for using the PHP code tags.