PHP Sensitive word Filter Premium Edition

Source: Internet
Author: User
Previously introduced a filter some special characters of the PHP program, below we upgrade this sensitive word filter function is more powerful with it no longer afraid of sensitive words in the middle of the space or other punctuation.


Advertisements or other sensitive words may appear wherever the user can speak, so a sensitive word filtering mechanism must be added to maintain the "purity" of the site.

Filter mechanism: Join PHP keyword regular match

$STR for user data
function Wordfilter ($STR)
{
/*
Get a list of sensitive words
How to store sensitive words:
1: Stored in TXT file (general method)
2: Store in cache (better method)
I was stored in the memcachd.
*/
$words = Getsensitivewords ();

foreach ($words as $word)
{
$preg _letter = '/^[a-za-z]+$/';
if (Preg_match ($preg _letter, $STR))
{//Match Chinese
$str = Strtolower ($STR);
$pattern _1 = '/([^a-za-z]+ '. $word. ' [^a-za-z]+] | ([^a-za-z]+ '. $word. ' \s+) | (\s+ '. $word. ' [^a-za-z]+] | (^ '. $word. ' [^a-za-z]+] | ([^a-za-z]+ '. $word. ' $)/';
Sensitive words are not empty on either side
if (Preg_match ($pattern _1, $STR))
{
$flag = TRUE;
}
$pattern _2 = '/(^ '. $word. ' \s+) | (\s+ '. $word. ' \s+) | (\s+ '. $word. '$)| (^ '. $word. '$)/';
You can have spaces on both sides of a sensitive word
if (Preg_match ($pattern _2, $STR))
{
$flag = TRUE;
}
}
Else
{//Match English strings, case insensitive
$pattern = '/\s* '. $word. ' \s*/';
if (Preg_match ($pattern, $STR))
{
$flag = TRUE;
}
}
}
}
A problem exists:

If you simply add keyword matches, users have a variety of anti-filtering methods, including intervening spaces or other punctuation.
Example:
Sensitive words: Buckle

After the user has processed:
Buttoning
Buttoning
Buckle @ Buckle
Buckle 1 Buckle
The regular match of the code may not match.

Workaround:

First, remove all punctuation and some special characters from the user data before making sensitive judgments.

Code:

$flag _arr=array ('? ','! ', ' ¥ ', ' (', ') ', ': ', ' ', ' ', ' ', ' ' ' ', ' ', ' ', ', ', ' ... ', '. ', ', ', ' nbsp ', ' ' ', ' ' ', ' ~ ');

$content _filter=preg_replace ('/\s/', ', Preg_replace ("/[[:p unct:]]/", ", Strip_tags (Html_entity_decode (str_ Replace ($flag _arr, ', $content), ent_quotes, ' UTF-8 ')));
$content _filter is the processed user data and then Wordfilter ($content _filter) filtering operation

  • 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.