PHP blacklist keyword filtering method

Source: Internet
Author: User
This article mainly introduces the PHP method for filtering the blacklist keywords. by saving the filter keyword to the txt file, you can read the contents of the txt file and perform regular match replacement to implement the filter function, for more information about how to filter blacklisted keywords in PHP, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:

Keyword filtering is very simple. the content to be filtered is stored in an array or a document. when the user submits the content, we can determine whether to use preg_match or istrpos. the specific method is as follows.

The instance code is as follows:

The code is as follows:

// Filter the blacklist keyword function
Function check _ ($ data ){
$ Keywords = "are you a great God | Hello | go to death ";
If (preg_match ("/$ keywords/I", $ data )){
Return;
} Else {
Return $ data;
}
}


The keyword is stored in txt and txt files in this form: | gambling machine | selling blood | selling kidney | selling organs | cornea, the code is as follows:

The code is as follows:

<? Php
Function Filter_word ($ str, $ fileName)
{
If (! ($ Words = file_get_contents ($ fileName ))){
Die ('file read error! ');
}
$ Str = strtolower ($ str );
// Var_dump ($ words );
$ Word = preg_replace ("/[1, 2, 3] |/I", '', $ words );
// $ Wor = substr ($ word, 0,-1 );
// $ W = preg_replace ("|/| I", '/', $ word );
// Echo"

";
// Var_dump ($ w );
// $ Words = "aaa | bbb | ccc | ddd | eee ";
$ Matched = preg_replace ('/'. $ word. '/I', '***', $ string );
Return $ matched;
}

$ Content = "I want aaafsdfbbb ";
If ($ result = Filter_word ($ content, './words.txt ')){
Echo $ result;
Echo "replaced successfully ";
} Else {
Echo "failed to replace! ";
?>

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.