phppreg-replace

How to reference the full string match in the replacement parameter of preg_replace()?


OK so I have this PHP script:

<?php

$stringz = "Dan likes to eat pears and his favorite color is green!";
$patterns = array("/pears/","/green/");
$string = preg_replace($patterns, '<b>\\1</b>', $stringz);
echo "<textarea rows='30' cols='100'>$string</textarea>";

?>

and when I run it I get this: Dan likes to eat <b></b> and his favorite color is <b></b>!

The is suppose to have a word in it... but it doesn't...


Solution

  • Change \\1 for \\0.