Use Strpos function in PHP to implement _php instance of masking sensitive keyword function

Source: Internet
Author: User
Now the network information supervision is very strict, especially the blocking keyword. Especially now WEB2.0 era, the content of the website is almost all from the Netizen publishes, the stationmaster manages can. If you want someone to prohibit publishing a keyword on your site, you need to do it beforehand. There are a variety of functional styles for keyword masking in PHP, such as regular is the most common one, here are not one by one examples, this article describes the use of PHP function Strpos blocking the keyword function.

Ideas:

First, the key words specifically written in a text file, each line one, the number of unlimited, how much write how much.
Second, PHP read the keyword text, stored in an array
Third, traverse the key word group, and then use the Strpos function to see if there is no keyword, if any, return true, no then return false.

The PHP code is as follows:
Copy the Code code as follows:
/**
* Use Strpos function to filter keywords in PHP
* Scripting House
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading the keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = explode ("\ n", $content);
Traverse 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, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if no keyword is detected
return false;
}


$content = ' Here is the text content to be published ... ';

Filter keywords
$keyWord = Keywordcheck ($content);

Determine if a keyword exists
if ($keyWord) {
Echo ' Your post has keywords '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
The publish action can be completed by the write library operation down.
}

After writing the code, deliberately write a keyword in the variable $content, and then run the discovery did not detect the keyword, the execution result is passed, replaced by other prohibited keywords are passed.

Depressed, began to judge is not where the problem.

Coding problems? Immediately open the KeyWord.txt file again in Notepad and save it as UTF-8 format. The result is still not working.

Didn't get the KeyWord.txt text content? Immediately print_r () finds a normal read and turns the array into a row.

So, I write the key word group directly into the dead in the program:
Copy CodeThe code is as follows:
<?php
/**
* Use Strpos function to filter keywords in PHP
* Scripting House
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading the keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = explode ("\ n", $content);
Declaring key word groups directly in your program
$arr = Array (' keyword 1 ', ' keyword 2 ', ' keyword 3 ', ' keywords 4 ' ...);
Traverse 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, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if no keyword is detected
return false;
}

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

Determine if a keyword exists
if ($keyWord) {
Echo ' The content you posted has the keyword '. $keyWord. ' 】';
}else{
Echo ' Congratulations! through keyword detection ';
The publish action can be completed by the write library operation down.
}
Program run Result: the content you post exists keyword "Keyword 2"
Program OK

If you declare a key word group in PHP, it will work, if reading a text file is invalid, what the hell?
At the time of the baffled, think of the text file can be read from a blank space or newline characters are not filtered caused? Then a trim function is added to the traversal match.

Add trim () function filter blank after running through the test, the original blind toss a half-day problem is here.
Copy the Code code as follows:
/**
* Use Strpos function to filter keywords in PHP
* Scripting House
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading the keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = explode ("\ n", $content);
Traverse 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, it returns a matching keyword and terminates the run
This time, the trim () function is added
if (@strpos ($str, Trim ($arr [$i]))!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if no keyword is detected
return false;
}


$content = ' Here is the text content to be published ... ';

Filter keywords
$keyWord = Keywordcheck ($content);

Determine if a keyword exists
if ($keyWord) {
Echo ' Your post has keywords '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
The publish action can be completed by the write library operation down.
}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.