smith.james0 Posted February 16, 2007 Share Posted February 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 16, 2007 Share Posted February 16, 2007 first, replace only works with single text, not with an array. secondly, i dont believe the output will be wat you expected. third, look up on strpos... O, i forgot how you spell it... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 16, 2007 Share Posted February 16, 2007 have you actually read the manual on preg_replace. here is a link: http://au2.php.net/preg_replace Quote Link to comment Share on other sites More sharing options...
smith.james0 Posted February 16, 2007 Author Share Posted February 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 16, 2007 Share Posted February 16, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 16, 2007 Share Posted February 16, 2007 try yours like this then: $answer = "car bus train rabbit computer"; $search = array("/rabbit/", "/computer/"); $answer2 = preg_replace($search,"replacement in here",$answer); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.