Jump to content

Complete Text Replacement


anasimtiaz

Recommended Posts

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

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.