PHP blocking methods for filtering specified keywords

Source: Internet
Author: User
This article mainly introduces PHP methods for blocking and filtering specified keywords, including techniques such as string filtering and array traversal, which are of great practical value, for more information about how to filter specified keywords in PHP, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Implementation 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 */
// 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, 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.
}

Example 2 (note: The keyword file used for Chinese keyword filtering is UTF-8 encoded)

The code is as follows:

/**
* Forbidden keyword detection
*
* @ Param string $ string the string to be checked
* @ Param string $ fileName mask keyword files
* @ Return bool
*/
Function banwordCheck ($ string, $ fileName)
{
If (! ($ Words = file_get_contents ($ fileName ))){
Die ('file read error! ');
}
$ String = strtolower ($ string );
$ Matched = preg_match ('/'. $ words. '/I', $ string, $ result );
If ($ matched & isset ($ result [0]) & strlen ($ result [0])> 0)
{
If (strlen ($ result [0]) = 2 ){
$ Matched = preg_match ('/'. $ words. '/iu', $ string, $ result );
}
If ($ matched & isset ($ result [0]) & strlen ($ result [0])> 0 ){
Return true;
} Else {
Return false;
}
} Else {
Return false;
}
}
$ Content = 'Test keyword ';
If (banwordCheck ($ content, './banwords.txt ')){
Echo "matched! ";
} Else {
Echo "no match! ";
}

I hope this article will help you with PHP programming.

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.