I am trying to create a regex to replace a string. Here is my pattern:
\{mailMerge: details_activity_number\\} When I do a search like this on a string like this:
hello {mailMerge: details_activity_number\}world its fine. But, if there's a line break like this:
hello \{ mailMerge: details_activity_number\} world it breaks. Here is my code in PHP:
$pattern = '\{mailMerge: 'mailMerge: details_activity_number'\}'; $content = str_replace($pattern, $value, $content); Can anyone help me with creating a pattern that would take into consideration possible line breaks/white spaces/etc to guarantee a match?
thanks
EDIT
private function findAndReplace($content, $mergedArray){ $test=$content; try{ foreach($mergedArray as $ArrayKey => $ArrayValue){ foreach ($ArrayValue as $key => $value) { $pattern = "\{\s*mailMerge:\s+". $key ."\s*\\\}"; if($value){ $test = preg_replace($pattern, $value, $test); }else{ $test = preg_replace($pattern, "No Value Exists", $test); } } } }catch(Exception $e){ throw $e; } return $test; }
\{and others you have{. Is the `\` required?str_replaceis for replacing literals. There are no regular expression pattern characters in$pattern.