PHP's method of determining spam comments by Chinese Character Ratio

Source: Internet
Author: User
This article mainly introduces PHP's method of determining spam comments through the Chinese character ratio. It is a practical technique and has some reference value for PHP application development, for more information, see

This article mainly introduces PHP's method of determining spam comments through the Chinese character ratio. It is a practical technique and has some reference value for PHP application development, for more information, see

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:

The two php functions strlen and mb_strlen are used. strlen considers the length of a single Chinese character as 3 and the length of a single Chinese character 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.

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.