When we do the message board often need to block keywords Some keywords, below I introduce the use of TXT save to block the keyword, and then based on the user submitted data filtering.
First look at the Strpos function
The Strpos () function returns the position of the first occurrence of a string in another string.
If the string is not found, false is returned.
Grammar
Strpos (string,find,start) parameter description
string is required. Specifies the string to be searched.
Find required. Specifies the character to find.
Start is optional. Specify where to start the search.
Hints and Notes
Note: The function is case-sensitive. Use the Stripos () function if you want to search for case insensitive.
Example
The code is as follows |
Copy Code |
Echo Strpos ("Hello world!", "Wo"); ?> |
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 are no keywords, if any, return true, no then return false
The PHP code is as follows
The code is as follows |
Copy Code |
< p>/** * Filter keywords with strpos function in PHP * qiong Tai Blog */ //keyword filter function function Keywordcheck ($content) { //Remove blank $ Content = Trim ($content); //Read keyword text $content = @file_get_contents (' keyWords.txt '); //convert 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 the keyword is detected, the matching keyword is returned and the run is terminated if (@strpos ($str, $arr [$i]) { //$i = $k;!==false return $arr [$i]; } } //returns False if no keyword is detected return false; } $content = ' Here is the text content to be published ... '; //filter keyword $keyWord = Keywordcheck ($content); //To determine if there is a keyword 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. } |
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, and began to judge if there was a 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 wrote the key word group directly into the dead in the program
The code is as follows |
Copy Code |
!--? php /** * Use Strpos function to filter keywords in php * qiong Tai Blog */ //keyword filter function function Keywordcheck ($content) { //Remove blank $content = Trim ($content), //Read keyword text //$content = @file_get_contents (' keyWords.txt '); //Convert to Array //$arr = Explode ("n", $content); //Declare the key word group $arr = Array (' keyword 1 ', ' keyword 2 ', ' keyword 3 ', ' keyword 4 ' ...) directly in the program. //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 the keyword is detected, the matching keyword is returned and the run is terminated if (@strpos ($str, $arr [$i]) { //$i = $k;!==false 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 keyword $keyWord = Keywordcheck ($content); //Determine if there is a keyword 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 posted has the 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.
The code is as follows |
Copy Code |
/** * Use Strpos function to filter keywords in PHP * Jones Taiwan Blog */ 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. } |
http://www.bkjia.com/PHPjc/629646.html www.bkjia.com true http://www.bkjia.com/PHPjc/629646.html techarticle when we do the message board often need to block keywords Some keywords, below I introduce the use of TXT save to block the keyword, and then based on the user submitted data filtering. First ...