0

i have prefixe for groupnames in a database, they consist of normal text and color codes eg.: &c[&aAdmin&c]. The colorcodes start with an & and then one character or a number. they end on the next colorcode or the end of the string. There are no & as text in the string. For now i have this function, but it only can handle one colorcode at the beginning of the string. any suggestions for a nice regex replace to handle multiple colorcodes in one string?

function mccolor($string){
$codes  = array(        "&0",
                        "&1",
                        "&2",
                        "&3",
                        "&4",
                        "&5",
                        "&6",
                        "&7",
                        "&8",
                        "&9",
                        "&a",
                        "&b",
                        "&c",
                        "&d",
                        "&e",
                        "&f");
$replace  = array(
                        '<span style="color:#000000;">',
                        '<span style="color:#0000BF;">',
                        '<span style="color:#00BF00;">',
                        '<span style="color:#00BFBF;">',
                        '<span style="color:#BF0000;">',
                        '<span style="color:#BF00BF;">',
                        '<span style="color:#BFBF00;">',
                        '<span style="color:#BFBFBF;">',
                        '<span style="color:#404040;">',
                        '<span style="color:#4040FF;">',
                        '<span style="color:#40FF40;">',
                        '<span style="color:#40FFFF;">',
                        '<span style="color:#FF4040;">',
                        '<span style="color:#FF40FF;">',
                        '<span style="color:#3F3F10;">',
                        '<span style="color:#FFFFFF;">');
return str_replace($codes, $replace, $string).'</span>';
}

2 Answers 2

2

How about:

function mccolor($m) {
    $trans = array(
        "&0"=>'#000000;',
        "&1"=>'#0000BF;',
        "&2"=>'#00BF00;',
        "&3"=>'#00BFBF;',
        "&4"=>'#BF0000;',
        "&5"=>'#BF00BF;',
        "&6"=>'#BFBF00;',
        "&7"=>'#BFBFBF;',
        "&8"=>'#404040;',
        "&9"=>'#4040FF;',
        "&a"=>'#40FF40;',
        "&b"=>'#40FFFF;',
        "&c"=>'#FF4040;',
        "&d"=>'#FF40FF;',
        "&e"=>'#3F3F10;',
        "&f"=>'#FFFFFF;',
    );
    return '<span style="color:'.$trans[$m[1]].'">'.$m[2].'</span>';
}
echo preg_replace_callback('/(&[0-9a-f])([^&]+|$)/', 'mccolor', '&c[&aAdmin&c]');

output:

<span style="color:#FF4040;">[</span><span style="color:#40FF40;">Admin</span><span style="color:#FF4040;">]</span>
Sign up to request clarification or add additional context in comments.

Comments

0

Or like that:

$str = 'i have prefixe for groupnames in a database, they consist of normal text and color codes eg.: &c[&aAdmin&c]. The colorcodes start with an & and then one character or a number. &1they &2end &3on &4the &5next &0colorcode or the end of the string. There are no & as text in the string. For now i have this function, but it only can handle one colorcode at the beginning of the string. any suggestions for a nice regex replace to handle multiple colorcodes in one string?';

$colors = array(
"&0" => '<span style="color:#000000;">',
"&1" => '<span style="color:#0000BF;">',
"&2" => '<span style="color:#00BF00;">',
"&3" => '<span style="color:#00BFBF;">',
"&4" => '<span style="color:#BF0000;">',
"&5" => '<span style="color:#BF00BF;">',
"&6" => '<span style="color:#BFBF00;">',
"&7" => '<span style="color:#BFBFBF;">',
"&8" => '<span style="color:#404040;">',
"&9" => '<span style="color:#4040FF;">',
"&a" => '<span style="color:#40FF40;">',
"&b" => '<span style="color:#40FFFF;">',
"&c" => '<span style="color:#FF4040;">',
"&d" => '<span style="color:#FF40FF;">',
"&e" => '<span style="color:#3F3F10;">',
"&f" => '<span style="color:#FFFFFF;">'
);

echo preg_replace("#&([0-9a-f])#ie", '$colors["&" . \\1]', $str);

1 Comment

This will not put any </span> after text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.