Jump to content

Quick preg_replace question


smith.james0

Recommended Posts

Why doesn't this work?

 

$answer = "car bus train rabbit computer";

 

$search = array('rabbit', 'computer');

 

$answer2 =  preg_replace($search,"<font color=\"red\">$term</font>",$answer);

 

preg_replace is not case sensitve. rabbit or computer doesn't change colour?

 

Many thanks James

Link to comment
https://forums.phpfreaks.com/topic/38744-quick-preg_replace-question/
Share on other sites

preg_replace works with arrays.

 

$string = 'The quick brown fox jumped over the lazy dog.';

$patterns[0] = '/quick/';

$patterns[1] = '/brown/';

$patterns[2] = '/fox/';

$replacements[2] = 'bear';

$replacements[1] = 'black';

$replacements[0] = 'slow';

echo preg_replace($patterns, $replacements, $string);

 

 

I was adapting this.

 

James

Your terms in the arrays are not regular expressions and you don't reference the matches like that...
[code[
$answer = "car bus train rabbit computer";

$search = array('/(rabbit)/', '/(computer)/');

$answer2 =  preg_replace($search,'<font color="red">$1<font>',$answer);
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.