Use the strpos function in PHP to block sensitive keywords _ php instance

Source: Internet
Author: User
This article mainly introduces how to use the strpos function in PHP to shield sensitive keywords. This article is used to intercept sensitive keys before publishing articles are saved to the database, for more information, see the strict supervision on network information, especially blocking keywords. In the web era, almost all website content is published by netizens and managed by webmasters. If you want someone to prohibit the publishing of a keyword on your site, you need to handle it in advance. There are a variety of functional styles for keyword shielding using PHP. For example, regular expressions are the most common. Here we will not illustrate them one by one. This article introduces the function of using the PHP function strpos to shield keywords.

Ideas:

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:


/**
* Use the strpos function in PHP to filter keywords.
* Foot home
*/
// 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.

As a result, I directly declare the keyword array as dead in the program:

The Code is as follows:


<? Php
/**
* Use the strpos function in PHP to filter keywords.
* Foot home
*/
// 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:


/**
* Use the strpos function in PHP to filter keywords.
* Foot home
*/
// 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.