This article describes how to implement the PHP blocking keyword. There are two ways to implement the first method.
Use regular expressions to match keywords and replace them with other characters.
$ Str = "/Hello | goodbye | what | DY/"; // keyword regular string $ string = "what have you done? "; // Text string echo preg_replace ($ str," * ", $ string); // preg_replace () executes a regular expression to match and replace
Method 2
Ideas
1. put keywords in a txt document and use the file_get_contents () function to read keywords from the document.
2. use the function explode () to split the string into an array loop array strpos () to find matching keywords.
Code
Header ('content-type: textml; charset = utf-8; '); function strPosFuck ($ content) {$ fuck = file_get_contents('keyWords.txt '); // read the keyword text information $ content = trim ($ content); $ fuckArr = explode ("\ n", $ fuck ); // Convert the keyword to an array for ($ I = 0; $ I <count ($ fuckArr); $ I ++) {// $ fuckArr [$ I] = trim ($ fuckArr [$ I]); if ($ fuckArr [$ I] = "") {continue; // skip this loop if the keyword is null # code ...} if (strpos ($ content, trim ($ fuckArr [$ I])! = False) {return $ fuckArr [$ I]; // if a keyword is matched, the keyword # code...} is returned ...}} return false; // return false if no matching keyword exists} $ content = "I met a SB today"; $ key = strPosFuck ($ content); if ($ key) {echo "keyword ". $ key; # code ...} else {echo "OK ";}
Be sure to leave it empty,
When the value returned by the strops () function is either false or the keyword location is determined, pay attention
After success, you can think about how to make up a string or array by returning all the matched keywords.