PHP 过滤不可识别字符

不可识别的字符在存mysql数据库,一定格式的编码不支持特殊字符的,有可能会报错,我整理下常用的字符。

/**
 * 过滤名称的不识别字符
 *
 * @param $name
 * @return string
 */
public static function filterMedicineName($name)
{

    /**
     * 过滤除中文,英文,数字,以及其他特殊字符以外的所有不识别的字符。
     * 特殊字符[`~!#$^&*()=|{}':;',\\[\\].<>/?~!#¥……&*()——|{}【】‘;:”“'。,、?]‘’"
     */
    $name = preg_replace(
        '/[^\x{4e00}-\x{9fa5}_a-zA-Z0-9\x{005b}\x{0060}\x{007e}\x{0021}\x{0023}' .
        '\x{0024}\x{005e}\x{0026}\x{002a}\x{0028}\x{0029}\x{003d}\x{007c}\x{007b}' .
        '\x{007d}\x{0026}\x{0023}\x{0033}\x{0039}\x{003b}\x{003a}\x{003b}\x{0026}' .
        '\x{0023}\x{0033}\x{0039}\x{003b}\x{002c}\x{005c}\x{005c}\x{005b}\x{005c}' .
        '\x{005c}\x{005d}\x{002e}\x{0026}\x{006c}\x{0074}\x{003b}\x{0026}\x{0067}' .
        '\x{0074}\x{003b}\x{002f}\x{003f}\x{007e}\x{ff01}\x{0023}\x{ffe5}\x{2026}' .
        '\x{2026}\x{0026}\x{002a}\x{ff08}\x{ff09}\x{2014}\x{2014}\x{007c}\x{007b}' .
        '\x{007d}\x{3010}\x{3011}\x{2018}\x{ff1b}\x{ff1a}\x{201d}\x{201c}\x{0026}' .
        '\x{0023}\x{0033}\x{0039}\x{003b}\x{3002}\x{ff0c}\x{3001}\x{ff1f}\x{005d}' .
        '\x{2018}\x{2019}\x{0026}\x{0071}\x{0075}\x{006f}\x{0074}\x{003b}]/u',
        '',
        $name
    );
    $name = trim($name);
    return $name;

}

本文已经被查看784次

发表评论

电子邮件地址不会被公开。 必填项已用*标注