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; 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. 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. Link to comment https://forums.phpfreaks.com/topic/51403-str-replace-despite-capitalization/#findComment-253150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.