The online racing simulator
[PHP] LFS Color Codes -> HTML tag
(6 posts, started )
[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/
#Edit
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

[PHP] LFS Color Codes -> HTML tag
(6 posts, started )
FGED GREDG RDFGDR GSFDG