Use the strpos function in PHP to implement the function of blocking sensitive keywords, strpos keyword_PHP tutorial

WBOY
Release: 2016-07-13 10:20:24
Original
1643 people have browsed it

The strpos function is used in PHP to implement the function of blocking sensitive keywords. The strpos keyword

Nowadays, network information supervision is very strict, especially blocking keywords. Especially in the current WEB2.0 era, almost all website content comes from netizens and can be managed by the webmaster. If you want others to prohibit publishing a certain keyword on your site, you need to do it in advance. There are various functional styles for using PHP to block keywords. For example, regular expression is the most common one. I will not list them one by one here. This article introduces the function of using the PHP function strpos to block keywords.

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:

Copy code The code is as follows:

/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
​*/
// 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 the 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,$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';
// You can perform the operation of the database to complete the release action.
}


After writing the code, I deliberately wrote a keyword content in the variable $content, and then ran it and found that no keyword was detected. The execution result was a pass, and other prohibited keywords passed.

Depressed, I started to judge whether there was something wrong.

Encoding problem? Immediately open the keyWord.txt file again with Notepad, and then save it in UTF-8 format. The result still didn't work.

Didn’t get the text content of keyWord.txt? Immediately print_r() found that it was read normally and converted into an array line by line.

So, I declared the keyword array directly in the program:


Copy code The code is as follows:

/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
​*/
// 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);
​​​​ // Declare the keyword array 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++){
// If the 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,$arr[$i])!==false){
                     //$i=$k;                                                      return $arr[$i];
                                                                      }
// Return false if no keyword is detected
Return false;
}

$content = 'Here is the content to be published, containing keyword 2';
// Filter keywords
$keyWord = keyWordCheck($content);

// Determine whether the keyword exists
if($keyWord){
echo 'The content you posted contains the keyword ['.$keyWord.']';
}else{
echo 'Congratulations! By keyword detection';
// You can perform the operation of the database to complete the release action.
}
// Program execution result: The content you posted contains the keyword [Keyword 2]
// The program is normal


If you declare an array of keywords in PHP, it works, but if you read a text file, it doesn't work. What the heck?
When I was puzzled, I thought, could it be caused by the content read from the text file having spaces or line breaks not being filtered? So a trim function was added to the traversal matching.

After adding the trim() function to filter out the blanks, the test passed. It turned out that the problem was here after messing around for a long time.


Copy code The code is as follows:

/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
​*/
// 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 the array element is empty, skip this loop
If($arr[$i]==''){
Continue;
}

// If a keyword is detected, return the matching keyword and terminate the operation
// This time I added the trim () function
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';
// You can perform the operation of the database to complete the release action.
}


Problem with php strpos function

Use 3 equal signs: ===. This requirement is more stringent. Not only the values ​​must be the same, but also the value types must be the same to return true


Strpos function problem in php

The third parameter means where to start~~
But the returned result still calculates the position based on the complete string!


$test = "whello world";
print (strpos($text,"w"));return 0

print(strpos($text,"w",1 ));return 7


http://www.bkjia.com/PHPjc/867247.html

truehttp: //www.bkjia.com/PHPjc/867247.htmlTechArticleThe strpos function is used in PHP to implement the function of blocking sensitive keywords. The strpos keyword is now very strict in network information supervision, especially Block keywords. Especially in the current WEB2.0 era, the content of the website...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!