php通过正则过滤非法的汉字或字符串|php通过正则过滤非法的汉字或字符

更新时间:2019-08-28    来源:php常用代码    手机版     字体:

【www.bbyears.com--php常用代码】

非法的汉字会影响到显示甚至程序的执行,会出现一些意想不到的结果。所以我们需要过滤这些非法的汉字或字符。
代码如下

 代码如下

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.bbyears.com/jiaocheng/64708.html

热门标签

更多>>

本类排行