Now the network information regulation is very strict, especially the shielding keyword. Especially now WEB2.0 times, the content of the website almost all comes from the Netizen publishes, Stationmaster manages can. If you want someone to stop publishing a keyword at your site, you need to do it beforehand. Use PHP to do keyword shielding function style has a variety of, such as is the most common one, here on no one by one cases, this article describes the use of PHP functions strpos mask keyword function.
Ideas:
First, the key words written in a text file, each line one, the number of unlimited, how many write.
Second, PHP read the keyword text, stored in an array
Three, traverse the key word group, each use Strpos function to see the content has no keyword, if there is, return True, no return false.
The PHP code is as follows:
Copy Code code as follows:
/**
* Use Strpos function in PHP to filter keywords
* Cloud-dwelling communities
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an 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, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if the keyword is not detected
return false;
}
$content = ' Here is the text content to be published ... ';
Filter keywords
$keyWord = Keywordcheck ($content);
To determine if a keyword exists
if ($keyWord) {
Echo ' What you're posting there is a keyword '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}
After writing the code, intentionally wrote a keyword content in the variable $content, and then ran the discovery did not detect the keyword, the execution result is passed, replaces the other prohibited keyword to pass.
Depressed, start to judge is not where the problem.
Coding problems? Immediately open the KeyWord.txt file in Notepad again and save it as a UTF-8 format. The result is still not good.
Not getting keyWord.txt text content? Print_r () immediately discovers the normal read and turns the array into rows.
So I wrote the key word group directly to the dead in the program:
Copy Code code as follows:
<?php
/**
* Use Strpos function in PHP to filter keywords
* Cloud-dwelling communities
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = explode ("\ n", $content);
Declare key word groups directly in your program
$arr = Array (' keyword 1 ', ' keyword 2 ', ' keyword 3 ', ' keyword 4 ' ...);
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, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if the keyword is not detected
return false;
}
$content = ' Here is the content to be published, containing the keyword 2 ';
Filter keywords
$keyWord = Keywordcheck ($content);
To determine if a keyword exists
if ($keyWord) {
Echo ' What you have published is the keyword '. $keyWord. ' 】';
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}
Program Run Result: You published the content of the keyword "key word 2"
Program Normal
If you declare a key word group in PHP, you can play a role, if you read the text file is not valid, hell?
At the time of baffled, think of the text file can be read from the contents of a space or newline characters do not filter caused? Then a trim function is added to the traversal match.
Add the trim () function filtered blank after running through the test, the original blind toss a half-day problem right here.
Copy Code code as follows:
/**
* Use Strpos function in PHP to filter keywords
* Cloud-dwelling communities
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an 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, 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 the keyword is not detected
return false;
}
$content = ' Here is the text content to be published ... ';
Filter keywords
$keyWord = Keywordcheck ($content);
To determine if a keyword exists
if ($keyWord) {
Echo ' What you're posting there is a keyword '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}