Home > Article > Backend Development > Summary of implementation methods for shielding and filtering specified keywords in PHP_PHP Tutorial
We often find that where we submit a reservation, there will be a lot of advertisements. Later, I wanted to make a function of blocking and filtering specified keywords. I searched for several methods and introduced them to everyone who needs to know. Friends can refer to it.
Things:
1. Write the keywords specifically in a text file, one per line. There is no limit to the number. Write as many as you want.
2. PHP reads the keyword text and stores it in an array
3. Traverse the keyword array and use the strpos function one by one to see if there are keywords in the content. If there are keywords, return true, if not, return false
The PHP code is as follows:
The code is as follows
| Copy code
| ||||
|
// Keyword filter function
//Read keyword text
} }The code is as follows<🎜> | Copy code<🎜> <🎜> |
<🎜><🎜>
/**<🎜>
* Banned keyword detection<🎜>
*<🎜>
* @param string $string The string to be detected <🎜>
* @param string $fileName Mask keyword file<🎜>
* @return bool<🎜>
*/<🎜>
function banwordCheck( $string, $fileName )<🎜>
{<🎜>
if ( !($words = file_get_contents( $fileName )) ){<🎜>
die('file read error!');<🎜>
}<🎜>
$string = strtolower($string);<🎜>
$matched = preg_match('/'.$words.'/i', $string, $result);<🎜>
if ( $matched && isset($result[0]) && strlen($result[0]) > 0 )
{
if ( strlen($result[0]) == 2 ){
$matched = preg_match('/'.$words.'/iu', $string, $result);
}
if ( $matched && isset($result[0]) && strlen($result[0]) > 0 ) {
Return true;
}else{
Return false;
}
}else{
return false;
}
}
$content = 'Test keyword';
if ( banwordCheck($content, './banwords.txt') ){
echo "matched! ";
}else{
echo "no match! ";
}
http://www.bkjia.com/PHPjc/629632.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629632.htmlTechArticleWe often find that where we submit a reservation, there will be a lot of advertising, and later we want to make one I have searched for several methods to introduce the function of blocking and filtering specified keywords...
Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn Previous article:preg_replace hidden backdoor and subsequent exploration_PHP tutorialNext article:preg_replace hidden backdoor and subsequent exploration_PHP tutorial Related articlesSee more |