PHP uses the Chinese character ratio to determine the method of spam comments. _ PHP Tutorial

Source: Internet
Author: User
PHP uses the Chinese character ratio to identify spam comments ,. PHP uses the Chinese character ratio to identify spam comments. this example describes how PHP uses the Chinese character ratio to determine spam comments. I would like to share with you how PHP judges spam comments by comparing Chinese characters,

This example describes how PHP judges spam comments based on the Chinese character ratio. Share it with you for your reference. The specific implementation method is as follows:

I. requirements:

Recently, this type of spam comments often appear: a large segment of English characters contains one or two uncommon Chinese characters, including Chinese characters, and does not contain any sensitive Chinese words, therefore, the comment was filtered out. The processing of such comments can be confirmed by determining the ratio of Chinese characters, but there will be some false positives.

II. solution:

To use two php functions strlen and mb_strlen, strlen considers a single Chinese character length as 3 and mb_strlen considers a single Chinese character length as 1. The difference in length between two functions for a single character is twice the number of actual Chinese characters. divide by two to get the actual number of characters, the ratio of the length obtained with mb_strlen is used to obtain the ratio of Chinese characters to the total number of characters.

III. implementation code:

The code is as follows:

$ Len_all = strlen ($ comment ['text']);
$ Len_st = mb_strlen ($ comment ['text'], 'utf-8 ');
If ($ len_all-$ len_st)/(2 * $ len_st) <0.5 ){
$ Error = "The number of Chinese characters is less than 50% ";
}

If you paste the code in the comments, the Chinese character rate will be low. you need to filter out the code fields and then judge.

I hope this article will help you with PHP programming.




Example: This article describes how PHP judges spam comments by comparing Chinese characters. Share it with you for your reference...

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.