Home > Backend Development > PHP Tutorial > How to shield and filter specified keywords in PHP, PHP shields and filters keywords_PHP tutorial

How to shield and filter specified keywords in PHP, PHP shields and filters keywords_PHP tutorial

WBOY
Release: 2016-07-13 10:15:24
Original
1038 people have browsed it

PHP shields and filters the method of specifying keywords, php shields and filters keywords

The example in this article describes the method of blocking and filtering specified keywords in PHP. Share it with everyone for your reference. The specific analysis is as follows:

Implementation ideas:

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:

Copy code The code is as follows:
/* Use strpos function to filter keywords in PHP */
// Keyword filter function
function keyWordCheck($content){
// Remove white space
$content = trim($content);
//Read keyword text
$content = @file_get_contents('keyWords.txt');
//Convert to array
$arr = explode("n", $content);
// Traversal detection
for($i=0,$k=count($arr);$i<$k;$i++){
// If this array element is empty, skip this loop
if($arr[$i]==''){
continue;
}
// If a keyword is detected, return the matching keyword and terminate the operation
if(@strpos($str,trim($arr[$i]))!==false){
//$i=$k;
return $arr[$i];
}
}
// Return false if no keyword is detected
return false;
}
$content = 'Here is the text content to be published. . . ';
// Filter keywords
$keyWord = keyWordCheck($content);
// Determine whether the keyword exists
if($keyWord){
echo 'The content you posted contains keywords'.$keyWord;
}else{
echo 'Congratulations! By keyword detection';
// Next, you can perform the library writing operation to complete the publishing action.
}

Example 2 (Note: The keyword file used when filtering Chinese keywords is utf-8 encoded)

Copy code The code is as follows:
/**
* 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! ";
}

I hope this article will be helpful to everyone’s PHP programming design.

I want to block some sensitive words through php code, but when I write broken code, I always get errors

To determine whether a certain text contains a certain string or certain strings, you can use preg_match to judge. For message books or forums, you can judge whether it contains certain sensitive keywords to decide whether to filter and allow it. Publish

Separate each sensitive word with the symbol "|", because in the regular expression "|" is equivalent to the meaning of "or"


$ badkey = "sensitive word|sensitive word B|sensitive word C";

$string = "I do not contain sensitive words, I want to publish";

if(preg_match("/ $badkey/i",$string)){

echo "Sorry, it contains sensitive characters and is not allowed to be published";

}else{

//do something ...

}

?>

And preg_replace can be used for keyword filtering (dirty words filtering)

php can use preg_replace Filter bad words


$badstring="tmd|damn|TNND|her motherfucker";
$string="What did you tmd say, her motherfucker, Not a human";
echo preg_replace("/$badstring/i",'',$string);

?>


$allergicWord = array('cussing','cussing');

$str = 'This sentence contains swearing and swearing';

for ($i=0; $i $content = substr_count($str, $allergicWord[$i]);
if($content>0){
$info = $ content;
break;
}
}

if($info>0){
//There are illegal characters
return TRUE;
}else{
//No illegal characters
return FALSE;
}
?>

How to use PHP to block keywords? You can also use js or ajax


Version:

html>
head>
/head>
form action="" method="post" name="form1" >
input name="word" type="text" />
input name="" type="submit" onclick="return chkValue();" value="Confirm"/>
/form>
script type="text/javascript">
function chkValue() {
var val = document.form1.word.value,
array = ["acd","svd"," fdg","fdk"];
for( var i = array.length; i--;) {
if( val.indexOf( array[i] ) !== -1 ) {
alert('There are sensitive characters:' + array[i]);
return false;
}
}
return true;
}
/script>
/html> ;

PHP version
?php

$content = "afjvahsv sjv sdjavcvacvdjgfsacdaldkfja sfcjasldfj ";
$keyword = array( "acd","svd","fdg", "fdk" );
for( $i = count($keyword); $i--; ) {
if( strpos( $content, $keyword[$i] ) !== false ) {
echo "Sensitive characters appear: {$keyword[$i]}";exit();
}
}

?>

A hundred generations of contemporary times are good for eternity Jiangshan Jinzhao New Vientiane Update

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904936.htmlTechArticleHow to shield and filter specified keywords in PHP, php shields and filters keywords. This article describes the example of PHP shielding and filtering specified keywords. method. Share it with everyone for your reference. The specific analysis is as follows:...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template