正则表达式过滤如'\xF0\x9F\x98'16进制字符

JS实现:

var str = "ss123ss\xF0\x9F\x98\x82\xF0\x9F";
str = str.replace(/[^\\x00-xFF]/g,"");
console.log(str);//ss123ss

 

PHP实现:

$str = "ss123ss\xF0\x9F\x98\x82\xF0\x9F";

$str = preg_replace("/[^\\x00-xFF]/m", "", $str);

echo $str."<br />";//ss123ss

 

1 if(!preg_match_all("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/mu",$str,$matches)){ //UTF-8汉字字母数字下划线正则表达式,如果不匹配,证明字符串非全中文和字母,可能含有标点符号或者表情符号
2     $str = preg_replace("/[^\\x00-xFF]/m", "", $str);//执行过滤
3 }

 

posted @ 2017-07-31 13:59  SingleShadow  阅读(1535)  评论(0编辑  收藏  举报