The online racing simulator
PHP4/5 - LFS Color Code Handing Functions (ECMA-48, HTML & Strip)
I recommend that you put the string first through the functions in this forum post and then through these for the best result.

Live For Speed Functions
ECMA-48 Escape String (Linux Console)

<?php 
# Convert Live For Speed String into an ECMA-48 escaped string.
function lfs_str_convert_console($str)
{
    
// Replace Set
    
$replaceArray = array("\033[30m""\033[31m""\033[32m""\033[33m""\033[34m""\033[35m""\033[36m""\033[37m""\033[39m""\033[0m");
    
// Extra State Information
    
$isTagOpen FALSE;
    
// Parse String
    
for ($i 0$j 1$l strlen($str); $i $l; ++$i, ++$j)
    {
        
/* Handle Color Codes */
        
if ($str{$i} == '^' && is_numeric($str{$j}))
        {
            
// Get array values' length.
            
$deltaLen strlen($replaceArray[$str[$j]]);
            
// Set tag status
            
if ($str{$j} < 8)
                
$isTagOpen TRUE;
            else
                
$isTagOpen FALSE;
            
// Place ECMA-48 Charaters into String.
            
$str substr($str0$i) . $replaceArray[$str[$j]] . substr($str$i);
            
// Move Pointer Past The Change.
            
$i += $deltaLen$j += $deltaLen;
            
// Get new String Length.
            
$l strlen($str);
            
// Remove The Formatting.
            
$str[$i] = NULL# Remove ^
            
$str[$j] = NULL# Remove Int.
        
}
    }
    
// Close any tag left open.
    
if ($isTagOpen)
        
$str .= "\033[0m";
    return 
$str;
}
?>

HTML Markup

<?php 
# Convert Live For Speed String into an HTML Mark'ed Up String.
function lfs_str_convert_html($str)
{
    
// Replace Set
    
$replaceArray = array('000''F00''0F0''FF0''00F''F0F''0FF''FFF');
    
// Extra State Information
    
$isTagOpen FALSE;
    
// Parse String
    
for ($i 0$j 1$l strlen($str); $i $l; ++$i, ++$j)
    {
        
/* Handle Color Codes */
        
if ($str{$i} == '^' && is_numeric($str{$j}))
        {
            if (
$isTagOpen == TRUE)
            {
                
// Set State.
                
$isTagOpen FALSE;
                
// Inject Close Tag
                
$str substr($str0$i) . '</span>' substr($str$i);
                
// Move Str pointers Past Δ.
                
$i += 7$j += 7$l strlen($str);
            }
            if (
$str{$j} < 8)
            {
                
// Set State
                
$isTagOpen TRUE;
                
// Inject HTML Markup
                
$str substr($str0$i) . '<span style="color: #'.$replaceArray[$str[$j]].';">' substr($str$i);
                
// Move Pointer Past The Change.
                
$i += 27$j += 27;
                
// Get new String Length.
                
$l strlen($str);
            }
            
// Remove The Formatting.
            
$str[$i] = NULL# Remove ^.
            
$str[$j] = NULL# Remove Int.
        
}
    }
    
// Close any tag left open.
    
if ($isTagOpen)
        
$str .= '</span>';
    return 
$str;
}
?>

Strip all LFS codes

<?php 
# Strip All Live For Speed Markup From The String.
function lfs_str_strip($str)
{
    return 
str_replace(array('^0','^1','^2','^3','^4','^5','^6','^7','^8','^9'), array(), str_replace(array('^d','^s','^c','^a','^q','^t','^l','^r','^v'), array('\\','/',':','*','?','"','<','>','|'), $str));
}
?>

#2 - amp88
Good stuff. I'll probably never use them but it's always good to see people doing work in the community.

I have a noob question about the code though...I must preface this by saying I don't know much about PHP and I've never written any before. There are a few cases (quoted below) where it looks like you're building up an array of constant data every time a function is called. Does PHP allow you to create static data types (so the constant data is only actually created once no matter how many times the function is called)?

Quote from Dygear :

<?php 
    $ARY 
= array("\033[30m""\033[31m""\033[32m""\033[33m""\033[34m""\033[35m""\033[36m""\033[37m""\033[39m""\033[0m");

    
$ARY = array('000''F00''0F0''FF0''00F''F0F''0FF''FFF');

    
$lfsCodes = array('^0''^1''^2''^3''^4''^5''^6''^7''^8''^9');
?>


Quote from amp88 :There are a few cases (quoted below) where it looks like you're building up an array of constant data every time a function is called. Does PHP allow you to create static data types (so the constant data is only actually created once no matter how many times the function is called)?

Not without converting this into a more classy, no pun intended, library.

You can only use consts within the context of a object, not within regular userland scope.

Further reading: constants and class constants.
#4 - amp88
OK, thanks for the info
A class with static arrays for the specific color data as well as static methods for the color handling would be a solution.

Data would be have only loaded once no matter how often you call the methods and the methods would be logically grouped together.

Might sound strange but this object oriented approach should produce less overhead than the original version.

The only drawback afaik would be no php4 support.
Quote from yankman :The only drawback afaik would be no php4 support.

Hardly a drawback as php4 is dead and has been for a while.
Quote from morpha :Hardly a drawback as php4 is dead and has been for a while.

I'm not so sure about this ... however I do feel that I can make the next version of LFSWorldSDK PHP5 only. Indeed, I'm shooting for 5.3 as the bare minimum spec. I guess I should get a an idea of what my audience is.
Quote from morpha :Hardly a drawback as php4 is dead and has been for a while.

Don't be to sure, php applications are part of my work and from time to time there are customers who still use php 4.
Mostly caused by very outdated linux distributions.

Anyway my idea was something like this.

<?php 
class LFSStrConvert
{
    
// Replace Sets
    
public static $consoleReplaceArray = array("\033[30m""\033[31m""\033[32m""\033[33m""\033[34m""\033[35m""\033[36m""\033[37m""\033[39m""\033[0m");
    public static 
$htmlReplaceArray = array('000''F00''0F0''FF0''00F''F0F''0FF''FFF');

    
# Convert Live For Speed String into an ECMA-48 escaped string.
    
public static function lfs_str_convert_console($str)
    {
        
// Extra State Information
        
$isTagOpen FALSE;
        
// Parse String
        
for ($i 0$j 1$l strlen($str); $i $l; ++$i, ++$j)
        {
            
/* Handle Color Codes */
            
if ($str{$i} == '^' && is_numeric($str{$j}))
            {
                
// Get array values' length.
                
$deltaLen strlen(LFSStrConvert::consoleReplaceArray[$str[$j]]);
                
// Set tag status
                
if ($str{$j} < 8)
                    
$isTagOpen TRUE;
                else
                    
$isTagOpen FALSE;
                
// Place ECMA-48 Charaters into String.
                
$str substr($str0$i) . LFSStrConvert::consoleReplaceArray[$str[$j]] . substr($str$i);
                
// Move Pointer Past The Change.
                
$i += $deltaLen$j += $deltaLen;
                
// Get new String Length.
                
$l strlen($str);
                
// Remove The Formatting.
                
$str[$i] = NULL# Remove ^
                
$str[$j] = NULL# Remove Int.
            
}
        }
        
// Close any tag left open.
        
if ($isTagOpen)
            
$str .= "\033[0m";
        return 
$str;
    }

    
# Convert Live For Speed String into an HTML Mark'ed Up String.
    
public static function lfs_str_convert_html($str)
    {
        
// Extra State Information
        
$isTagOpen FALSE;
        
// Parse String
        
for ($i 0$j 1$l strlen($str); $i $l; ++$i, ++$j)
        {
            
/* Handle Color Codes */
            
if ($str{$i} == '^' && is_numeric($str{$j}))
            {
                if (
$isTagOpen == TRUE)
                {
                    
// Set State.
                    
$isTagOpen FALSE;
                    
// Inject Close Tag
                    
$str substr($str0$i) . '</span>' substr($str$i);
                    
// Move Str pointers Past ?.
                    
$i += 7$j += 7$l strlen($str);
                }
                if (
$str{$j} < 8)
                {
                    
// Set State
                    
$isTagOpen TRUE;
                    
// Inject HTML Markup
                    
$str substr($str0$i) . '<span style="color: #'.LFSStrConvert::htmlReplaceArray[$str[$j]].';">' substr($str$i);
                    
// Move Pointer Past The Change.
                    
$i += 27$j += 27;
                    
// Get new String Length.
                    
$l strlen($str);
                }
                
// Remove The Formatting.
                
$str[$i] = NULL# Remove ^.
                
$str[$j] = NULL# Remove Int.
            
}
        }
        
// Close any tag left open.
        
if ($isTagOpen)
            
$str .= '</span>';
        return 
$str;
    }

    
# Strip All Live For Speed Markup From The String.
    
function lfs_str_strip($str)
    {
        return 
str_replace(
            array(
'^0','^1','^2','^3','^4','^5','^6','^7','^8','^9'),
            array(),
            
str_replace(array('^d','^s','^c','^a','^q','^t','^l','^r','^v'),
            array(
'\\','/',':','*','?','"','<','>','|'), $str)
        );
    }      
}
?>

Would be nice to do some profiling, comparing both approaches.
I wouldn't be surprised if the php interpreter recognizes the constant arrays within the functions and does it only initialize once.
It's dead in every way, not even mentioned anywhere on php.net. It's not being developed anymore, it's not supported anymore, it's not even available for download from the project's own website (although probably still in the svn repo)! Let it go.

@yankman: What reason could someone possibly have to intentionally use outdated software? That's very unprofessional if you ask me
Quote from yankman :Don't be to sure, php applications are part of my work and from time to time there are customers who still use php 4.
Mostly caused by very outdated linux distributions.

Anyway my idea was something like this.

[REDACTED]

Would be nice to do some profiling, comparing both approaches.
I wouldn't be surprised if the php interpreter recognizes the constant arrays within the functions and does it only initialize once.

I already have a PHP5 version of this, that's also name spaced.
Quote from morpha :@yankman: What reason could someone possibly have to intentionally use outdated software? That's very unprofessional if you ask me

I don't want this to become far off topic, so a short and last explanation.

There are many privateers out there, who startet to rent root-servers (bundled with confixx/plesk etc.) for cutting the costs for hosting multiple sites.
They don't care about linux or php version (actually they don't have an idea what that is). The main goal is keep the server runing at minimal costs, so they run for years (personally I know 2 such guys). As often as I tell them they need to upgrade at least for security reasons,
they are afraid of the costs, the downtime and what else might happen.

So my experience, php 4 might be dead in terms of support but not in terms of usage in servers out there.

FGED GREDG RDFGDR GSFDG