zartzar Posted March 16, 2009 Share Posted March 16, 2009 str_replace(replace_this, with_this, $in_this); is it possible to have multiple replace_this's in a single str_replace? Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/ Share on other sites More sharing options...
jackpf Posted March 16, 2009 Share Posted March 16, 2009 You probably want to use preg_replace() which can replace with arrays. Look it up in the manual; it's pretty easy to comprehend. Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/#findComment-786277 Share on other sites More sharing options...
Zane Posted March 16, 2009 Share Posted March 16, 2009 use an array $string = "The dog is chasing the cat who eats fish and chases a mouse"; $search = array('dog','fish','cat','mouse'); $replace = array('bear','shark','lion','deer'); $string = str_replace($search, $replace, $string); echo $string; Yeilds The bear is chasing the lion who eats shark and chases a deer. Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/#findComment-786278 Share on other sites More sharing options...
zartzar Posted March 16, 2009 Author Share Posted March 16, 2009 use an array $string = "The dog is chasing the cat who eats fish and chases a mouse"; $search = array('dog','fish','cat','mouse'); $replace = array('bear','shark','lion','deer'); $string = str_replace($search, $replace, $string); echo $string; Yeilds The bear is chasing the lion who eats shark and chases a deer. That works frickin sweet! Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/#findComment-786285 Share on other sites More sharing options...
imperium2335 Posted March 17, 2009 Share Posted March 17, 2009 This made me think, could you also apply this to a chat/forum word filter to remove bad, racists etc words? $message = $_REQUEST['msg'] ; $badwordlist = include 'mybadwords.html' ; $search = array('$badwordlist'); $replace = array('flowers','happy'); $message= str_replace($search, $replace, $message); echo $message; Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/#findComment-786358 Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 Short answer yes. Long answer no. Regular expressions would be better since you can use word boundaries. Ass -> assassination. for example Quote Link to comment https://forums.phpfreaks.com/topic/149731-solved-str_replace/#findComment-786359 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.