I want some ideas on how to parse LFS strings in PHP. Right now I'm using a concoction of Victor's LFS Hostname Codepage Converter & D34N0's Format Host Colour.
That now looks like this.
We can do better then this. So, let's see what ya got!
That now looks like this.
<?php
# Function by Mark 'Dygear' Tomlin;
function hostToHTML($hostName) {
return format_host_colours(codepage_convert($hostName));
}
# Function By Victor van Vlaardingen: http://www.lfsforum.net/showthread.php?t=36628
function codepage_convert($str, $conv_to = 'UTF-8') {
$sets = array (
'L' => 'CP1252',
'G' => 'ISO-8859-7',
'C' => 'CP1251',
'E' => 'ISO-8859-2',
'T' => 'ISO-8859-9',
'B' => 'ISO-8859-13',
'J' => 'SJIS-win',
'S' => 'CP936',
'K' => 'CP949',
'H' => 'CP950'
);
$tr_ptrn = array ("/\^d/", "/\^s/", "/\^c/", "/\^a/", "/\^q/", "/\^t/", "/\^l/", "/\^r/", "/\^v/");
$tr_ptrn_r = array ("\\", "/", ":", "*", "?", "\"", "<", ">", "|");
$str = preg_replace ($tr_ptrn, $tr_ptrn_r, $str);
$newstr = $tmp = '';
$current_cp = 'L';
$len = strlen ($str);
for ($i=0; $i<$len; $i++) {
if ($str{$i} == '^' && isset ($sets[$str{$i+1}]) && $str{$i-1} != "^") {
if ($tmp != '') {
$newstr .= mb_convert_encoding ($tmp, $conv_to, $sets[$current_cp]);
$tmp = '';
}
$current_cp = $str{++$i};
} else if (ord($str{$i}) > 31)
$tmp .= $str{$i};
}
if ($tmp != '')
$newstr .= mb_convert_encoding ($tmp, $conv_to, $sets[$current_cp]);
return str_replace ('^^', '^', $newstr);
}
# Function by D34N0: http://www.lfsforum.net/showthread.php?p=35947#post35947
# Function Edited by Mark 'Dygear' Tomlin;
function get_colour($ColourNum) {
switch ($ColourNum) {
case 0: return '#000000'; # Black
case 1: return '#FF0000'; # Red
case 2: return '#00FF00'; # Green
case 3: return '#FFFF00'; # Yellow
case 4: return '#0000FF'; # Light Blue
case 5: return '#FF0080'; # Light Purple
case 6: return '#00FFFF'; # Turquoise
case 7; return '#FFFFFF'; # White
case 8: return '#00FF00'; # Pastel Green
}
}
# Function by D34N0: http://www.lfsforum.net/showthread.php?p=35947#post35947
# Funntion Edited by Mark 'Dygear' Tomlin;
function format_host_colours($HostName) {
for ($i = 0; $i < strlen($HostName); $i++) {
if (substr($HostName, $i, 1) == "^") {
$CharPos = strpos($HostName, "^", $i);
$ColNum = substr($HostName, strpos($HostName, "^", $i) + 1,1);
$ColourString = get_colour(substr($HostName, strpos($HostName, "^", $i) + 1,1));
if ($i == "0") {
$TmpString = substr($HostName,$i+2);
$HostName = "<span style=\"color: {$ColourString}\">{$TmpString}";
} else {
$LTmpString = substr($HostName,0,$i);
$RTmpString = substr($HostName,$i+2);
$HostName = "{$LTmpString}</span><span style=\"color: {$ColourString}\">{$RTmpString}";
}
}
}
$HostName .= '</span>';
return $HostName;
}
?>