非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。
代码如下
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, 感谢原作者分享。
