RE: [PHP] preg_replace

From: Date: Wed, 19 Feb 2003 02:34:22 +0000
Subject: RE: [PHP] preg_replace
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-136105@lists.php.net to get a copy of this message
> This should work (as far as I can tell) But yields the following error. > Parse error: parse error, unexpected T_CHARACTER, expecting T_STRING or > T_VARIABLE or T_NUM_STRING > Any help is appreciated! > > function HTMLfromNickName ( $player ) { > //example input "^2pla^3yer" > $color=array( > "1"=>"#FF0000", > "2"=>"#00FF00", > "3"=>"#F0FF0F", > "4"=>"#0000FF", > "5"=>"#00FFFF", > "6"=>"#FF00FF", > "7"=>"#FFFFFF", > "8"=>"ltbrown", > "9"=>"#0F00F0", > "0"=>"#E7E7E7" > ); > $pattern = "/\^(\d)/"; > $replacement = "$color[$1]"; > $player = preg_replace($pattern, $replacement, $player); > return $player; > } You have a $1, which is not valid unless it's inside a "replacement" string. It's not where you have it. Anyway, you need an 'e' modifier to make this work. Use these two lines: $pattern = "/\^(\d)/e"; $replacement = '$color[$1]'; ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/

« previous php.general (#136105) next »