豆包AI编程
智能代码生成与优化,高效提升开发速度与质量!
php判断中文和编码有关 gbk是双字节,utf8是三字节,可以根据 中文的范围来判断
编码范围1. GBK (GB2312/GB18030)
\x00-\xff GBK双字节编码范围
\x20-\x7f ASCII
\xa1-\xff 中文
\x80-\xff 中文
2. UTF-8 (Unicode)
\u4e00-\u9fa5 (中文)
\x3130-\x318F (韩文
\xAC00-\xD7A3 (韩文)
\u0800-\u4e00 (日文)
ps: 韩文是大于[\u9fa5]的字符
正则例子:
preg_replace(”/([\x80-\xff])/”,”",$str);
preg_replace(”/([u4e00-u9fa5])/”,”",$str);
二、代码例子
代码如下:
//判断内容里有没有中文-GBK (PHP) function check_is_chinese($s){ return preg_match('/[\x80-\xff]./', $s); } //获取字符串长度-GBK (PHP) function gb_strlen($str){ $count = 0; for($i=0; $i<strlen if return function for> 127) { $count++; if($value >= 192 && $value = 224 && $value = 240 && $value = $position && $start_position > $i){ $start_position = $i; $start_byte = $count; } if(($count-$start_byte)>=$length) { $end_position = $i; break; } $value = ord($str[$i]); if($value > 127){ $count++; if($value >= 192 && $value = 224 && $value = 240 && $value 0x3130 && str.charCodeAt(i) = 0xAC00 && str.charCodeAt(i) </strlen>
php免费学习视频:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!
已抢6799个
抢已抢91603个
抢已抢14417个
抢已抢50597个
抢已抢190549个
抢已抢86251个
抢