This article mainly introduces the PHP method for filtering the blacklist keywords. by saving the filter keyword to the txt file, you can read the contents of the txt file and perform regular match replacement to implement the filter function, for more information about how to filter blacklisted keywords in PHP, see the example in this article. 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.