通过正则过滤非法的汉字或字符
Jun 06, 2016 pm 08:14 PM非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。 代码如下 function normalizeText($text, $length = null){ $text = \Normalizer::normalize($text, \Normalizer::FORM_C); $text = preg_replace
非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。
代码如下
function normalizeText($text, $length = null) { $text = \Normalizer::normalize($text, \Normalizer::FORM_C); $text = preg_replace('/[^\p{L}\p{P}\p{N}\p{S}\p{Zs}]/u', "", $text); $text = preg_replace('/^\p{Z}*/u', "", $text); $text = preg_replace('/\p{Z}*$/u', "", $text); if ($length !== null) { $text = mb_substr($text, 0, $length, 'utf-8'); } return $text; }
本文出自:http://www.jxm.cc, 原文地址:http://www.jxm.cc/blogs/527.html, 感谢原作者分享。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How effective is receiving phone calls using airplane mode?

VBOX_E_OBJECT_NOT_FOUND(0x80bb0001)VirtualBox error

Reasons and solutions for desktop layout being locked

How to open filtered duplicate files in Quark

What problems will bad sectors on the hard drive cause?

How to match multiple words or strings using Golang regular expression?

How to turn off the comment function on TikTok? What happens after turning off the comment function on TikTok?

Correct way to display Chinese characters in matplotlib
