PHP中特殊符号会影响页面布局以及导出表格乱码,此方法用以过滤特殊字符号和表情符号,
/** * 替换微信名中的表情符号 * text string 要替换的文字 * replaceTo string 替换成的文字 */ function filterEmoji(string $text='', string $replaceTo = '') { $clean_text = ""; // Match Emoticons $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u'; $clean_text = preg_replace($regexEmoticons, $replaceTo, $text); // Match Miscellaneous Symbols and Pictographs $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u'; $clean_text = preg_replace($regexSymbols, $replaceTo, $clean_text); // Match Transport And Map Symbols $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u'; $clean_text = preg_replace($regexTransport, $replaceTo, $clean_text); // Match Miscellaneous Symbols $regexMisc = '/[\x{2600}-\x{26FF}]/u'; $clean_text = preg_replace($regexMisc, $replaceTo, $clean_text); // Match Dingbats $regexDingbats = '/[\x{2700}-\x{27BF}]/u'; $clean_text = preg_replace($regexDingbats, $replaceTo, $clean_text); return self::reeChar($clean_text); } /** * 过滤特殊字符 删除空格与回车,去除特殊字符 */ function reeChar(string $strParam='') { $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|/"; $qian = array(" ", " ", "\t", "\n", "\r"); $cv = preg_replace($regex, "", $strParam); return str_replace($qian, '', $cv); }
原文链接:https://blog.csdn.net/weixin_41031687/article/details/82794761