zfred09 Posted May 14, 2007 Share Posted May 14, 2007 How would I replace a string despite capitalization? Say I have this code. $searchterm = "cool"; $text = "Hi, my names Cool."; $newtext = str_replace("$searchterm", "<FONT style=\"BACKGROUND-COLOR: yellow\">$searchterm</FONT>", "$text"); echo $newtext; Quote Link to comment https://forums.phpfreaks.com/topic/51403-str-replace-despite-capitalization/ Share on other sites More sharing options...
btherl Posted May 14, 2007 Share Posted May 14, 2007 Try this: $newtext = preg_replace("|$searchterm|i", "<FONT style=\"BACKGROUND-COLOR: yellow\">$searchterm</FONT>", "$text"); Changes are: 1. str_replace => preg_replace 2. "$searchterm" => "|$searchterm|i" The "i" means "case-insensitive", which is the change you wanted to make. If I had written "|$searchterm|", it would act exactly like str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/51403-str-replace-despite-capitalization/#findComment-253140 Share on other sites More sharing options...
zfred09 Posted May 15, 2007 Author Share Posted May 15, 2007 Worked perfectly thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/51403-str-replace-despite-capitalization/#findComment-253150 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.