Using strpos function in PHP to realize the function of shielding sensitive keywords

  • 2021-07-13 04:43:02
  • OfStack

Nowadays, the supervision of network information is very strict, especially shielding keywords. Especially in the WEB2.0 era, the content of the website is almost all released by netizens, and the webmaster can manage it. If you want others to ban a keyword on your site, you need to deal with it in advance. There are various functional styles of keyword masking with PHP, such as regularity is the most common one, so there are not 11 examples here. This paper introduces the function of using PHP function strpos to mask keywords.

Thoughts:

1. Write keywords in a text file, one per line, and write as many as possible.
2. PHP reads the keyword text and stores it in an array
3. Traverse the keyword array, and use strpos function one by one to see if there are keywords in the content. If there are, return true, and if there is no, return false.

The PHP code is as follows:


/**
 * PHP Chinese use strpos Function filtering keywords
 * Script House
 */
// Keyword filter function
function keyWordCheck($content){
        // Remove blanks
    $content = trim($content);
        // Read keyword text
    $content = @file_get_contents('keyWords.txt');
        // Convert to an array
    $arr = explode("\n", $content);
        // Traversal detection
    for($i=0,$k=count($arr);$i<$k;$i++){
                // Skip this loop if this array element is empty
        if($arr[$i]==''){
              continue;   
        }
 
                // If a keyword is detected, a matching keyword is returned , And terminate the operation
        if(@strpos($str,$arr[$i])!==false){
            //$i=$k;   
            return $arr[$i];
        }   
    }
        // Returns if no keyword is detected false   
    return false;
}
 
 
$content = ' Here is the text content to be published. . . ';
 
// Filter keywords
$keyWord =  keyWordCheck($content);
 
// Determining whether a keyword exists
if($keyWord){
        echo ' There are keywords in the content you publish '.$keyWord;
}else{
        echo ' Congratulations! Pass keyword detection ';
        // Next, you can write the library to complete the publishing action.
}

After writing the code, deliberately in the variable $content to write a keyword content, and then run found that no keyword was detected, the execution result is through, replaced by other prohibited keywords are passed.

Depressed, began to judge what went wrong.

Coding problem? Immediately open the keyWord. txt file again in Notepad and save it as UTF-8 format. It still doesn't work.

Did you get keyWord. txt text content? Immediately print_r () finds that it reads normally and turns into an array by row.

So, I wrote the keyword array directly as dead in the program:


<?php
/**
 * PHP Chinese use strpos Function filtering keywords
 * Script House
 */
// Keyword filter function
function keyWordCheck($content){
        // Remove blanks
    $content = trim($content);
        // Read keyword text
    //$content = @file_get_contents('keyWords.txt');
        // Convert to an array
    //$arr = explode("\n", $content);
        // Declare keyword arrays directly in the program
        $arr = array(' Keyword 1',' Keyword 2',' Keyword 3',' Keyword 4'...);
        // Traversal detection
    for($i=0,$k=count($arr);$i<$k;$i++){
                // Skip this loop if this array element is empty
        if($arr[$i]==''){
              continue;   
        }
 
                // If a keyword is detected, a matching keyword is returned , And terminate the operation
        if(@strpos($str,$arr[$i])!==false){
            //$i=$k;   
            return $arr[$i];
        }   
    }
        // Returns if no keyword is detected false   
    return false;
}
 
$content = ' Here is the content to be published, with keywords 2';
// Filter keywords
$keyWord =  keyWordCheck($content);
 
// Determining whether a keyword exists
if($keyWord){
        echo ' The content you publish has keywords " '.$keyWord.' " ';
}else{
        echo ' Congratulations! Pass keyword detection ';
        // Next, you can write the library to complete the publishing action.
}
// Program results: You publish the content has the keyword "keyword 2 "
// The program is normal

If you declare a keyword array in PHP, it will work. If you read a text file, it will be invalid. What the hell?
When I was puzzled, I wondered if it was caused by the contents read from the text file having spaces or the newline characters not being filtered. So an trim function is added to the traversal match.

Added trim () function filter blank after running through the test, the original messing around for half a day problem is here.


/**
 * PHP Chinese use strpos Function filtering keywords
 * Script House
 */
// Keyword filter function
function keyWordCheck($content){
        // Remove blanks
    $content = trim($content);
        // Read keyword text
    $content = @file_get_contents('keyWords.txt');
        // Convert to an array
    $arr = explode("\n", $content);
        // Traversal detection
    for($i=0,$k=count($arr);$i<$k;$i++){
                // Skip this loop if this array element is empty
        if($arr[$i]==''){
              continue;   
        }
 
                // If a keyword is detected, a matching keyword is returned , And terminate the operation
                // This 1 Added for the second time trim() Function
        if(@strpos($str,trim($arr[$i]))!==false){
            //$i=$k;   
            return $arr[$i];
        }   
    }
        // Returns if no keyword is detected false   
    return false;
}
 
 
$content = ' Here is the text content to be published. . . ';
 
// Filter keywords
$keyWord =  keyWordCheck($content);
 
// Determining whether a keyword exists
if($keyWord){
        echo ' There are keywords in the content you publish '.$keyWord;
}else{
        echo ' Congratulations! Pass keyword detection ';
        // Next, you can write the library to complete the publishing action.
}


Related articles: