2012-01-13 41 views
0

我已經prefixe用於在數據庫組名的,它們包括普通的文本和顏色代碼例如: & C [& aAdmin & C]。顏色代碼以&開頭,然後是一個字符或數字。他們結束在下一個顏色代碼或字符串的結尾。 字符串中沒有&作爲文本。 現在我有這個功能,但它只能處理字符串開頭的一個顏色代碼。任何建議一個很好的正則表達式替換處理一個字符串中的多個顏色代碼?正則表達式對多個取代的ColorCode

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

如何:

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]'); 

輸出:

<span style="color:#FF4040;">[</span><span style="color:#40FF40;">Admin</span><span style="color:#FF4040;">]</span> 
+0

非常感謝,做工精細。 – Abadon 2012-01-13 14:48:29

+0

@abadon:不客氣。 – Toto 2012-01-13 15:07:17

0

或類似的:

$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); 
+0

這不會在文字後加上任何''。 – Toto 2012-01-13 15:09:21