This article mainly introduces the PHP Method for filtering blacklist keywords. By saving the filter keyword to the txt file, you can read the content of the txt file and perform regular match replacement to implement the filter function. For more information, see
This article mainly introduces the PHP Method for filtering blacklist keywords. By saving the filter keyword to the txt file, you can read the content of the txt file and perform regular match replacement to implement the filter function. For more information, see
This example describes how to filter the blacklist keyword in PHP. Share it with you for your reference. The specific implementation method is as follows:
Keyword filtering is very simple. The content to be filtered is stored in an array or a document. When the user submits the content, we can determine whether to use preg_match or istrpos. The specific method is as follows.
The instance code is as follows:
The Code is as follows:
// Filter the blacklist keyword Function
Function check _ ($ data ){
$ Keywords = "Are you a great god | Hello | go to death ";
If (preg_match ("/$ keywords/I", $ data )){
Return;
} Else {
Return $ data;
}
}
The keyword is stored in txt and txt files in this form: | gambling machine | selling blood | selling kidney | selling organs | cornea, the Code is as follows:
The Code is as follows:
<? Php
Function Filter_word ($ str, $ fileName)
{
If (! ($ Words = file_get_contents ($ fileName ))){
Die ('file read error! ');
}
$ Str = strtolower ($ str );
// Var_dump ($ words );
$ Word = preg_replace ("/[1, 2, 3] |/I", '', $ words );
// $ Wor = substr ($ word, 0,-1 );
// $ W = preg_replace ("|/| I", '/', $ word );
// Echo"
";
// Var_dump ($ w );
// $ Words = "aaa | bbb | ccc | ddd | eee ";
$ Matched = preg_replace ('/'. $ word. '/I', '***', $ string );
Return $ matched;
}
$ Content = "I want aaafsdfbbb ";
If ($ result = Filter_word ($ content, './words.txt ')){
Echo $ result;
Echo "replaced successfully ";
} Else {
Echo "failed to replace! ";
?>I hope this article will help you with PHP programming.
,