Bladescope Posted May 2, 2009 Share Posted May 2, 2009 Hey PHPFreaks again, I have an array of keywords and a string. What I would like to know is how to create a function for the following: • to search for each of the keywords in the string, no matter the case • if a keyword/keywords is/are found within the string, to add a prefix and a suffix to the word/s whilst still maintaining the case of the word. • to return the string with all of the words replaced I wouldn't ask for a full script, but any help would be greatly appreciated. Thanks in advance, Bladescope Link to comment https://forums.phpfreaks.com/topic/156550-adding-a-prefixsuffix-to-one-or-more-words-in-a-string/ Share on other sites More sharing options...
hchsk Posted May 2, 2009 Share Posted May 2, 2009 i believe a this will do what you need if you place it in a loop if (stristr($string, $searchforme)) { str_replace($string, substr ( $string , (stristr($string, $searchforme)), 0 ), substr ( $string , (stristr($string, $searchforme)), 0 )+strlen($prefix) ); } or for suffix if (stristr($string, $searchforme)) { str_replace($string, substr ( $string , (stristr($string, $searchforme)), 0 )+strlen($searchforme), substr ( $string , (stristr($string, $searchforme)), 0 )+strlen($suffix) ); } obviously replace the variables with your variables, and you may still need to modify the code, this is untested hopefully this at least gives you the nudge you need Link to comment https://forums.phpfreaks.com/topic/156550-adding-a-prefixsuffix-to-one-or-more-words-in-a-string/#findComment-824296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.