search
Homephp教程php手册PHP实现通过中文字符比率来判断垃圾评论的方法

这篇文章主要介绍了PHP实现通过中文字符比率来判断垃圾评论的方法,是一个比较实用的技巧,对于进行PHP应用程序开发来说有一定的参考借鉴价值,需要的朋友可以参考

本文实例讲述了PHP实现通过中文字符比率来判断垃圾评论的方法。分享给大家供大家参考。具体实现方法如下:

一、需求:

最近一段时间常常出现这类垃圾评论:一大段英文字符里夹杂一两个生僻汉字,包含了中文字符,而且又没包含啥中文的敏感词,所以就堂而皇之的通过了评论过滤。对这类评论的处理可以采取判断中文字符的比率来确认,但是也会存在一定的误判。

二、解决方案:

要用到php的两个函数strlen和mb_strlen,,strlen会把单个汉字长度认定为3,mb_strlen单个汉字长度为1。同一段字符通过两个函数取得的长度之差就是实际汉字字符数的二倍,除以二就得到实际的字符数,在与mb_strlen取得的长度求比值就得到汉字占总字符数的比率。

三、实现代码:

复制代码 代码如下:

$len_all = strlen($comment['text']);                     
 $len_st = mb_strlen($comment['text'], 'UTF-8');
 if(($len_all-$len_st)/(2*$len_st)         $error = "中文字符少于百分之五十"; 
 }

如果在评论中贴代码的话,就会造成中文字符比率低,需要过滤掉代码字段再来判断。

希望本文所述对大家的PHP程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)