I'm formating for a chatting dialog bellow:
The bold text above ; it make error in my replace statment:
$name = self::getNameFromMessage($textFormat);
$color = self::getNewTextColor();
$replacement = "<br/><font color='$color'>$name</font>:";
$textFormat = preg_replace(
$pattern = "/$name :\s/i", //find $name in $textFormat and set text color
$replacement,
$subject = $textFormat
);
Error: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 15
Can you suggest me a better regex string?
The error was caused by not quoting your regex, in your example, the regex finds a (
, then it expects to find a closing )
. If you want to match a literal ()
or other regex meta-characters, you need to quote your regex:
$pattern = preg_quote("/$name :\s/i");