The online racing simulator
[PHP] LFS Color Codes -> HTML tag
I've written a basic PHP function to convert LFS color coded strings into a string using HTML
<span>

tags. See the code below:


<?php 
        
function lfsColorToHTML($text)
        {
        
                
$repred 0;
                
$repgreen 0;
                
$repyellow 0;
                
$repblue 0;
                
$reppink 0;
                
$replightblue 0;
                
$repwhite 0;
                
$repblack 0;
                
                
$htmlCode str_replace("^1""<span style='color:red'>"$text$repred);
                
$htmlCode str_replace("^2""<span style='color:green'>"$htmlCode$repgreen);
                
$htmlCode str_replace("^3""<span style='color:yellow'>"$htmlCode$repyellow);
                
$htmlCode str_replace("^4""<span style='color:blue'>"$htmlCode$repblue);
                
$htmlCode str_replace("^5""<span style='color:pink'>"$htmlCode$reppink);
                
$htmlCode str_replace("^6""<span style='color:lightblue'>"$htmlCode$replightblue);
                
$htmlCode str_replace("^7""<span style='color:white'>"$htmlCode$repwhite);
                
$htmlCode str_replace("^8""<span style='color:black'>"$htmlCode$repblack);
                
                
$totalrep $repred+$repgreen+$repyellow+$repblue+$reppink+$replightblue+$repwhite+$repblack;
                
$repcounter 0;
                
                while(
$repcounter $totalrep)
                {
                        
$htmlCode .= "</span>";
                        
$repcounter++;
                }
                
                return 
$htmlCode;
        }
?>

Usage:


<?php 

$myString 
"^1Hello ^2World. ^3Testing.";

$newString lfsColorToHTML($myString);

echo 
$newString//Result: <span style='color:red'>Hello <span style='color:green'>World. <span style='color:yellow'>Testing.</span></span></span>

?>

An example can be seen on my team website. The server color codes are converted using this function: https://www.team-vtec.ga/
<?php
function nade($pt) {
$fi=0; $nm = ""; $sp = array(
'v' => '|', 'a' => '*',
'c' => ':', 'd' => '\\',
's' => '/', 'q' => '?',
't' => '"', 'l' => '<',
'r' => '>', 'h' => '#',
'L' => '', 'G' => '',
'C' => '', 'J' => '',
'E' => '', 'T' => '',
'B' => '', 'H' => '',
'S' => '', 'K' => ''
); $cr = array(
9 => "#888",
8 => "#070", // D Green
7 => "#EEE", // White
6 => "#0EE", // L Blue
5 => "#D69", // Purple
4 => "#00E", // Blue
3 => "#FD0", // Yellow
2 => "#0E0", // L Green
1 => "#E33", // Red
0 => "#111" // Black

); $ar = explode("^", $pt);
foreach($ar as $nw) {
$add="";
if (!empty($nw)) {
$vl = $nw[0];
if (is_numeric($vl)) {
if ($vl >= 0 && $vl <= 9) {
if ($fi==0) {
$add='<span style="text-shadow: 0px 0px 8px #000;"><font color="'.$cr[$vl].'">'.substr($nw, 1);
}
else {
$add='</font><font color="'.$cr[$vl].'">'.substr($nw, 1);
}
}
}
else {
$add = $sp[$vl].substr($nw, 0);
}
}
$nm=$nm.$add;
}
return $nm;
}
?>

Yours is definitely more technically skilled than mine Tongue
Edit: As Dygear says; just use Victor's library at https://www.lfs.net/forum/post/1748707#post1748707


If you're not a fan of nesting your span's (which assuming you're putting your output through a html sanitiser might make it's job a little easier) you could do something like the following, which will result in;

<span style="color: red">Hello </span><span style="color: green">World. </span><span style="color: yellow">Testing.</span>


<?php

$str = "^1Hello ^2World. ^3Testing.";

echo preg_replace_callback("(\^([0-9])(.+?)(?=\^[0-9]|$))", function($match) {
$colours = array(
0 => 'black',
1 => 'red',
2 => 'green',
3 => 'yellow',
4 => 'blue',
5 => 'pink',
6 => 'lightblue',
7 => 'white',
8 => 'black',
9 => 'inherit'
);

return '<span style="color: '.$colours[$match[1]].'">'.$match[2].'</span>';
}, $str);
?>

It's been a long time since I've looked at LFS, but I could've sworn there was a method to escape ^. Not in the manual though and I can't be bothered to download the game to test.
-
asdas (MangisEvil) DELETED by Scawen : spam

FGED GREDG RDFGDR GSFDG