You can't have a special character as the key to an array without
surrounding it by quotes. Here's a solution to that and a better way to
organize your code.

If you define your array as such:

$color=array(
//numeric
"1"=>"#FF0000","2"=>"#00FF00","3"=>"#F0FF0F",
"4"=>"#0000FF","5"=>"#00FFFF","6"=>"#FF00FF",
"7"=>"#FFFFFF","8"=>"#C56D29","9"=>"#7B5131",
"0"=>"#E7E7E7",etc...

Just give the color value, not the extra HTML. You're just wasting space
when you can do that in your preg_replace.

Now, the preg_replace to do what you want will look like this:

$player = preg_replace("/\^(.)([^^]+)/ex", 'sprintf("<font
color=\"{$color[\'$1\']}\">$2</font>")', $player);

So a value such as

$player = 'Bas^#ket ^?Ball Pla^:yer';

will produce:

Bas<font color="#000000">ket </font><font color="#7BD2AD">Ball
Pla</font><font color="#63319C">yer</font>

Which is much better HTML output than you had before. Enjoy.

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/

> -----Original Message-----
> From: aw2001 [mailto:[EMAIL PROTECTED]
> Sent: Monday, February 24, 2003 9:30 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] preg_replace with /e modifier
> 
> I cant find a way to properly escape the special characters for php
code
> evaluation (iam sure its something simple) Can some one point this
out?
> 
> [errors generated]
> Parse error: parse error, unexpected ']'
> Fatal error: Failed evaluating code: $color[!]
> 
> [function containing error]
> function HTMLfromNickName ( $player ) {
> $color=array(
> //numeric
> "1"=>"<font color=#FF0000>","2"=>"<font color=#00FF00>","3"=>"<font
> color=#F0FF0F>","4"=>"<font color=#0000FF>","5"=>"<font
> color=#00FFFF>","6"=>"<font color=#FF00FF>","7"=>"<font
> color=#FFFFFF>","8"=>"<font color=#C56D29>","9"=>"<font
> color=#7B5131>","0"=>"<font color=#E7E7E7>",
> //Caps
> "Q"=>"<font color=#A5B2DE>","W"=>"<font color=#8C41E6>","E"=>"<font
> color=#DE4D21>","R"=>"<font color=#00E700>","T"=>"<font
> color=#2939CE>","Y"=>"<font color=#520473>","U"=>"<font
> color=#42EB31>","I"=>"<font color=#428629>","O"=>"<font
> color=#00CA73>","P"=>"<font color=#CE8A00>","A"=>"<font
> color=#7BCA4A>","S"=>"<font color=#E649B5>","D"=>"<font
> color=#42D23A>","F"=>"<font color=#29416B>","G"=>"<font
> color=#5A9221>","H"=>"<font color=#001442>","J"=>"<font
> color=#8C6D7B>","K"=>"<font color=#4ABACE>","L"=>"<font
> color=#8CA28C>","Z"=>"<font color=#19716B>","X"=>"<font
> color=#5A8E7B>","C"=>"<font color=#3A04BD>","V"=>"<font
> color=#8C3D3A>","B"=>"<font color=#A56900>","N"=>"<font
> color=#9469AD>","M"=>"<font color=#A5D77B>",
> //lower
> "q"=>"<font color=#A5656B>","w"=>"<font color=#291884>","e"=>"<font
> color=#AD35A5>","r"=>"<font color=#218E6B>","t"=>"<font
> color=#8C418C>","y"=>"<font color=#080408>","u"=>"<font
> color=#31007B>","i"=>"<font color=#4AA229>","o"=>"<font
> color=#00CE73>","p"=>"<font color=#3A6929>","a"=>"<font
> color=#BD0000>","s"=>"<font color=#847173>","d"=>"<font
> color=#D68E3A>","f"=>"<font color=#7396BD>","g"=>"<font
> color=#C5007B>","h"=>"<font color=#52969C>","j"=>"<font
> color=#730829>","k"=>"<font color=#21418C>","l"=>"<font
> color=#4AD208>","z"=>"<font color=#000819>","x"=>"<font
> color=#08A631>","c"=>"<font color=#08BA8C>","v"=>"<font
> color=#520821>","b"=>"<font color=#192473>","n"=>"<font
> color=#84AED6>","m"=>"<font color=#A5926B>",
> //special
> "!"=>"<font color=#313942>","@"=>"<font color=#197573>","#"=>"<font
> color=#000000>","\$"=>"<font color=#000000>","\%"=>"<font
> color=#000000>","\&"=>"<font color=#00E700>","*"=>"<font
> color=#088284>","("=>"<font color=#BDC629>",")"=>"<font
> color=#D614DE>","_"=>"<font color=#198A73>","-"=>"<font
> color=#198A73>","+"=>"<font color=#EFEFEF>","="=>"<font
> color=#C53DDE>","["=>"<font color=#6BA621>","]"=>"<font
> color=#3A927B>","{"=>"<font color=#215D7B>","}"=>"<font
> color=#840C00>","'"=>"<font color=#A5926B>","\""=>"<font
> color=#FFFFFF>","?"=>"<font color=#7BD2AD>","/"=>"<font
> color=#FFFFFF>","|"=>"<font color=#A5B629>",","=>"<font
> color=#7B8A8C>","."=>"<font color=#000000>","<"=>"<font
> color=#B5825A>",">"=>"<font color=#00A200>",":"=>"<font
> color=#63319C>",";"=>"<font color=#A5926B>"
> );
> //todo: include special characters
>   $end = $end + substr_count ($player, "^");
>   $player = preg_replace("/\^(.)/ex", '$color[$1]', $player);
>   $player .= str_repeat ("</font>", $end);
>   return $player;
> }
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to