Jump to content

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);
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.