The online racing simulator
How to get rid of color codes in insim packets?
How can i get rid of color codes in insim packets?

When i try to match a "PName" against a string it won't match if the player writes his name with colors...



And if you know an easier way to get the "player controlled car" uniqueid without having the user enter his playername (lfs-world nick is not set in sp) and comparing against NPL packeges, just tell me
just remove them with a selfmade function

be sure to check if there is a ^ in the name and check the following character!
if this is 1 to 8 the this should be a coulor code!
Its not elegant in the slightest, but since you didnt specify which language you're writing in, I just did it in InSim's "native".

Note: in this example you need to have allocated the buffer's memory before hand.
int stripcolours(const char *input, char *buffer, unsigned int size)
{
if (size < strlen(buffer))
return -1;

int inputsize;
inputsize = strlen(input);

if (inputsize < 0)
return -2;

int i, j;
for (i = 0, j = 0; i < inputsize; ++i)
{
if ((input[i] == '^') && (input[(i++)] >= '0') && (input[(i++)] <= '9'))
{
i += 2;
}

if ((i < strlen(input)) && (j <= size))
{
buffer[j] = input[i];
j++;
}
}

buffer[j] = '\0';

return 1;
}

P.S. I've not tested this, and I've written it on-the-fly, so please dont slaughter me if its wrong.
#4 - Stuff
Here is my little way in VB6 to get rid of the colour codes Goes from 0-9 which is more than the current colours, but if more are added later it will pick it up.


Public Function StripColors(ByVal strString As String) As String
On Error GoTo StripColorsErr
Dim i As Byte

For i = 0 To 9
strString = Replace(strString, "^" & CStr(i), "")
Next i

StripColors = strString

Exit Function
StripColorsErr:
Call Report("-ERROR- StripColors() - " & err.Number & ": " & err.Description)
End Function

For the player controlled part.. Do you mean which is an AI and which is not? If so, just check the type in the NPL packet. (byte Type; // bit 0 - female / bit 1 - AI) If its 2, its an AI.
ok i got rid of colour codes. didn't think about the ^x stuff as it changed with patch q

@Stuff: I connect to my local LFS and wand to retrieve my own lap/split times and not the ones of the other cars. So currently I compare username/lfsworld nick in NPL and LAP packets and use the uniqueid delivered with those packets...

String firstrange = "",lastrange = "";
String Chars1 = "vacdsqtlr";
String Chars2 = "|*:\\/?\"<>";
String Chars3 = "LGCJETB";
int index;


for(int i = 1; i < outpack.Length();i++)
{
if(outpack[i] == '^' && i + 1 <= outpack.Length())
{
if(StrToIntDef(outpack[i + 1],11112) != 11112)
{
// number, remove color codes
index = outpack.Pos("^");
if(index + 2 <= outpack.Length())
{
firstrange = outpack.SubString(1,index - 1);
lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));//die 2 byte ausschneiden
outpack = firstrange + lastrange;
}
}
else
{ // string split
index = outpack.Pos("^");
int pos = Chars1.Pos(outpack[index + 1]);
if(pos > 0 && index + 2 <= outpack.Length())
{
firstrange = outpack.SubString(1,index - 1) + Chars2[pos];
lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));
outpack = firstrange + lastrange;
}

//remove countrycode
index = outpack.Pos("^");
pos = Chars3.Pos(outpack[index + 1]);
if(pos > 0 && index + 2 <= outpack.Length())
{
firstrange = outpack.SubString(1,index - 1);
lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));//die 2 byte ausschneiden
outpack = firstrange + lastrange;
}
}
}
}

return outpack;

Seems a bit over complicated JogDive :o
#8 - filur

<?php 
preg_replace
("/\^[1-8]/","",$coloredName);
?>

"Regular expressions ftw"
Nevertheless, the most correct answer is given by JogDive.
/me points out that the question was only asking how to remove colour codes
Thus I refute that statement smith
Quote from smith :Nevertheless, the most correct answer is given by JogDive.

I think so too i wrote this function for the DediLink software and it works without any failure. But there are some things that could be optimized
The PHP Way.
Not perfect, but it will do the job you ask of it.


<?php 
php

/* Get rid of color codes in InSim packets
   Stand alone funcion (Requires no other function call) */
function lfs_strip_cc$str_name )
{
    
$arr_name explode'^'$str_name );
    foreach( 
$arr_name as $key => $arr_name_part )
    {
        if( 
is_numeric$arr_name_part{0} ) )
            
$arr_name[$key] = substr$arr_name_part);
    }
    
$name implode''$arr_name );
    return 
$name;
}

// First Attempt!
echo lfs_strip_cc'^1(E^7AGL^4E)^7Dygear' );

?>


<?php 
echo preg_replace("/\^[1-8]/","","^1(E^7AGL^4E)^7Dygear"); 
?>

All right, all right, all right, I'll give it a shot.
I have never used preg_replace before, so I did not know how your code would work. Also, look, I can turn it into html quite easly with my function . . .


<?php 
/*    Convert the colorcodes from LFS into a html color, using the font tag.
    Stand alone funcion (Requires no other function call)            */
function lfs_cc2html$str_name )
{
    
$arr_name explode'^'$str_name );
    foreach( 
$arr_name as $key => $arr_name_part )
    {
        if( 
is_numeric$arr_name_part{0} ) )
        {
            switch( 
$arr_name_part{0} )
            {
                case 
1:
                    
$arr_name[$key] = '<font color="#ff0000">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
2:
                    
$arr_name[$key] = '<font color="#05fd05">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
3:
                    
$arr_name[$key] = '<font color="#ffff00">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
4:
                    
$arr_name[$key] = '<font color="#0000ff">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
5:
                    
$arr_name[$key] = '<font color="#ff00ff">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
6:
                    
$arr_name[$key] = '<font color="#00ffff">' substr$arr_name[$key], ) . '</font>';
                    break;
                case 
7:
                    
$arr_name[$key] = '<font color="#ffffff">' substr$arr_name[$key], ) . '</font>';
                    break;
                default:
                    
$arr_name[$key] = substr$arr_name[$key], );
                    break;
            }
        }
    }
    
$name implode''$arr_name );
    return 
$name;
}

$tabs "\t\t";
echo 
$tabs lfs_cc2html'^1(E^7AGL^4E)^7Dygear' ) . "<br />\n";
?>


<?php 
php
  $name 
"^1(E^7AGL^4E)^7Dygear";
  
  function 
col($n) {
    
$colors = array('ff0000','05fd05','ffff00','0000ff','ff00ff','00ffff','ffffff');
    return 
$colors[$n-1];
  }
  echo 
preg_replace("/\^([1-8])(.[^\^]*)/e","'<font color=\"#'.col('\\1').'\"\\2</font>'",$name);

?>

Too many lines argh!

I wanted to get the color from the array directly in the regex, not using "col(n);", but i can't make PHP like back references as array keys.

$colors[\\1] simply returns value from key 1, not the value from subpattern-value-as-key. It also gives a warning.
Quote from filur :
I wanted to get the color from the array directly in the regex, not using "col(n);", but i can't make PHP like back references as array keys.

Just gave it a try ... and it works when you change " an ' in the replace-part, thus giving:

<?php 
$name 
"^1(E^7AGL^4E)^7Dygear";
$colors = array('000000','ff0000','05fd05','ffff00','0000ff','ff00ff','00ffff','ffffff','cccccc');
echo 
preg_replace("/\^([0-8])(.[^\^]*)/e",'"<font color=\"#".$colors[\\1]."\">\\2</font>"',$name);
?>

(Just included #000000 as color 0 and #cccccc as default ...)

FGED GREDG RDFGDR GSFDG