anasimtiaz Posted September 16, 2008 Share Posted September 16, 2008 i know that certain instances of a text can be replaced by another using str_replace but i want to accomplish this: IF a sentence contains any one of a word from a list that i make, then the whole sentence needs to be replaced by another sentence. For example, the words in a blacklist are blue, red, green. and the sentence is: this cup is blue it shud be replaced by: shut up!! Link to comment https://forums.phpfreaks.com/topic/124454-complete-text-replacement/ Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 Well I can't think of a function off the top of my head, so you can use a home made function. <?php function checkString($str){ //Specify bad words $bad = array("blue", "red", "green"); $exp = explode(" ", $str); $wordsFound = 0; foreach($exp as $word){ if(in_array($word, $bad)){ $wordsFound++; } } return ($wordsFound > 0 ? "Shut up!!" : $str); } $str = "this cup is blue"; echo checkString($str); ?> Link to comment https://forums.phpfreaks.com/topic/124454-complete-text-replacement/#findComment-642687 Share on other sites More sharing options...
anasimtiaz Posted September 16, 2008 Author Share Posted September 16, 2008 thanks a lot, works like a charm Link to comment https://forums.phpfreaks.com/topic/124454-complete-text-replacement/#findComment-642691 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.