Rahul Dev Posted December 17, 2010 Share Posted December 17, 2010 hi folks, i have a variable in which some common words are stored and another variable in which a sentence is stored. I want to remove the common words from the sentence> Is there any way of doing this in PHP? <?php $common_words = "to, this, all, the, from"; $sentence = "I want to remove all the common words from this sentence"; ?> the final output should be: "I remove common words sentence." Quote Link to comment https://forums.phpfreaks.com/topic/221976-remove-some-words-from-string/ Share on other sites More sharing options...
trq Posted December 17, 2010 Share Posted December 17, 2010 str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/221976-remove-some-words-from-string/#findComment-1148614 Share on other sites More sharing options...
Rahul Dev Posted December 17, 2010 Author Share Posted December 17, 2010 str_replace. when i use str_replace() i can't remove certain words correctly e.g my common_words contains the word "the" and if my sentence contians the word "they" then it would output "y" <?php $sentence = "I want to remove all the common words from this sentence because they are irrelevant"; $common_words = array("to", "this", "all", "the", "from"); $newphrase = str_replace($common_words, "", $sentence); echo $newphrase; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221976-remove-some-words-from-string/#findComment-1148631 Share on other sites More sharing options...
trq Posted December 17, 2010 Share Posted December 17, 2010 $common_words = array(" to ", " this ", " all ", " the ", " from "); Quote Link to comment https://forums.phpfreaks.com/topic/221976-remove-some-words-from-string/#findComment-1148634 Share on other sites More sharing options...
Rahul Dev Posted December 17, 2010 Author Share Posted December 17, 2010 thanx for the help! Quote Link to comment https://forums.phpfreaks.com/topic/221976-remove-some-words-from-string/#findComment-1148635 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.