PHP blocking keyword implementation method, php blocking keyword
This article describes how to implement the PHP blocking keyword. There are two implementation methods:
Method 1
Use regular expressions to match keywords and replace them with other characters.
$ Str = "/ | you are paralyzed | what things | SB | fuck/"; // keyword regular string $ string = "What the fuck are you doing SB "; // text string echo preg_replace ($ str, "*", $ string); // preg_replace () executes matching and replacement of a regular expression
The "OK" method is quite simple. When you are reading it, You can first think about it based on your own ideas.
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; // if no matching keyword exists, return false} $ content = "I met a SB today"; $ key = strPosFuck ($ content ); if ($ key) {echo "keyword ". $ key; # code ...} else {echo "OK ";}
NotesBe sure to leave it empty, 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.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.