PHP shields keyword programs using strpos Functions

Source: Internet
Author: User

When we make message boards, we often need to block some keywords. Below I will introduce the use of txt to save and block keywords, and then filter them based on the data submitted by the user.

First look at the strpos Function

The strpos () function returns the position where the string first appears in another string.

If this string is not found, false is returned.

Syntax

Strpos (string, find, start) parameter description
String is required. Specifies the string to be searched.
Find is required. Specifies the characters to be searched.
Start is optional. Specifies the location where the search starts.

Tips and comments

Note: This function is case sensitive. Use the stripos () function to perform case-insensitive searches.

Example

The Code is as follows: Copy code

<? Php
Echo strpos ("Hello world! "," Wo ");
?>

1. Write keywords in a text file. Each line has one keyword. The number of keywords is unlimited and the number of keywords is limited.

2. PHP reads the keyword text and stores it in an array

3. traverse the keyword array. Use the strpos function one by one to check whether there are any keywords in the content. If yes, return true. If no, return false.


The PHP code is as follows:

The Code is as follows: Copy code

/**
* Use the strpos function in PHP to filter keywords.
* Qiongtai blog
*/
// Keyword filtering function
Function keyWordCheck ($ content ){
// Remove the blank
$ Content = trim ($ content );
// Read the 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 the array element is null
If ($ arr [$ I] = ''){
Continue;
}
 
// If a keyword is detected, the matching keyword is returned and the running is terminated.
If (@ strpos ($ str, $ arr [$ I])! = False ){
// $ I = $ k;
Return $ arr [$ I];
}
}
// If no keyword is detected, false is returned.
Return false;
}
 
 
$ Content = 'Here is the text to be published... ';
 
// Filter keywords
$ KeyWord = keyWordCheck ($ content );
 
// Determine whether a keyword exists
If ($ keyWord ){
Echo 'your published content has a keyWord '. $ keyWord;
} Else {
Echo 'Congratulations! Keyword Detection ';
// You can write the database to complete the publishing.
}

 

After writing the code, I intentionally wrote a keyword content in the variable $ content, and then run it to find that no keyword is detected. The execution result is passed, and all other forbidden keywords are passed.
Depressed, start to determine if something went wrong
Encoding Problems? Immediately open the keyword.txt file again with notepad and save it as UTF-8 format. The result still does not work.
Didn't the keyword.txt text be obtained? Print_r () immediately finds that the data is read normally and converted to an array by row.
Therefore, I directly declare the keyword array as a dead object in the program.

The Code is as follows: Copy code

<? Php
/**
* Use the strpos function in PHP to filter keywords.
* Qiongtai blog
*/
// Keyword filtering function
Function keyWordCheck ($ content ){
// Remove the blank
$ Content = trim ($ content );
// Read the keyword text
// $ Content = @file_get_contents('keyWords.txt ');
// Convert to an array
// $ Arr = explode ("n", $ content );
// Declare a keyword array directly in the program
$ Arr = array ('tag1 ', 'tag2', 'tag3 ', 'tag4 '...);
// Traversal Detection
For ($ I = 0, $ k = count ($ arr); $ I <$ k; $ I ++ ){
// Skip this loop if the array element is null
If ($ arr [$ I] = ''){
Continue;
}
 
// If a keyword is detected, the matching keyword is returned and the running is terminated.
If (@ strpos ($ str, $ arr [$ I])! = False ){
// $ I = $ k;
Return $ arr [$ I];
}
}
// If no keyword is detected, false is returned.
Return false;
}
 
$ Content = 'here is the content to be published, containing the keyword 2 ';
// Filter keywords
$ KeyWord = keyWordCheck ($ content );
 
// Determine whether a keyword exists
If ($ keyWord ){
Echo 'your published content has a keyWord ['. $ keyWord .']';
} Else {
Echo 'Congratulations! Keyword Detection ';
// You can write the database to complete the publishing.
}
// Program running result: Your published content has a keyword [keyword 2]
// The program is normal.

If a keyword array is declared in PHP, it can be used. If a text file is read, it will be invalid?
When I was puzzled, I thought about whether there were spaces or linefeeds that were not filtered out from text files? Therefore, a trim function is added to traversal matching.

After the trim () function is added to the filter space, the system passes the test. It turns out that the problem has been around for a long time.

The Code is as follows: Copy code

 

/**
* Use the strpos function in PHP to filter keywords.
* Qiongtai blog
*/
// Keyword filtering function
Function keyWordCheck ($ content ){
// Remove the blank
$ Content = trim ($ content );
// Read the 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 the array element is null
If ($ arr [$ I] = ''){
Continue;
}
 
// If a keyword is detected, the matching keyword is returned and the running is terminated.
// This time the trim () function is added
If (@ strpos ($ str, trim ($ arr [$ I])! = False ){
// $ I = $ k;
Return $ arr [$ I];
}
}
// If no keyword is detected, false is returned.
Return false;
}
 
 
$ Content = 'Here is the text to be published... ';
 
// Filter keywords
$ KeyWord = keyWordCheck ($ content );
 
// Determine whether a keyword exists
If ($ keyWord ){
Echo 'your published content has a keyWord '. $ keyWord;
} Else {
Echo 'Congratulations! Keyword Detection ';
// You can write the database to complete the publishing.
}

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.