检测字符串编码 我想问一下,在php中能不能时时的检测是某个字符串使用的是什么编码呢?在网上搜一下,没有什么收获 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/yw1386/archive/2009/10/14/4671845.aspx看看这里 可以用editplus 字符串拷贝进去 右下角有字符编码的显示 裝個 mbstring 庫用mb_detect_encoding可以檢查字串的編碼 编码不是由字符串本身决定的....'a' 既是 ascii的,也是utf8的,也是latin1的,也是gbk的......0xe4 0xb8 0xa5 可以是中文严(utf8),也可以是严 (3个latin1字符).....mb_detect_encoding只是根据一些编码的规律大概去猜测编码...所以它的参数里是可以指定一个编码范围的.. 这个很多都检查不准哦,比如“喔唷”。<?phpfunction check_utf8($str) { $len = strlen($str); for($i = 0; $i < $len; $i++){ $c = ord($str[$i]); if ($c > 128) { if (($c > 247)) return false; elseif ($c > 239) $bytes = 4; elseif ($c > 223) $bytes = 3; elseif ($c > 191) $bytes = 2; else return false; if (($i + $bytes) > $len) return false; while ($bytes > 1) { $i++; $b = ord($str[$i]); if ($b < 128 || $b > 191) return false; $bytes--; } } } return true;} // end of check_utf8?>可以试试上面我写的这个! 弱弱问一句 memcache 存的信息满了会怎样 ? PHP 读取SQL output返回值问题 PHP与COM 一般我们做项目,最常修改php.ini 的什么配置呢? 问个常见的问题 有没有生成word文件的方法? "http:\/\/(\w{1,9})+.yeshere.com\/"为何不能匹配http://zairwolf.yeshere.com/?? 庆祝DFLying生日 放分 另外提个非技术问题 紧急求助,2个初级问题 header location 的页面跳转问题 请教正则表达式牛人啊 面试题,求解
看看这里
用mb_detect_encoding可以檢查字串的編碼
'a' 既是 ascii的,也是utf8的,也是latin1的,也是gbk的......
0xe4 0xb8 0xa5 可以是中文严(utf8),也可以是严 (3个latin1字符).....mb_detect_encoding只是根据一些编码的规律大概去猜测编码...所以它的参数里是可以指定一个编码范围的..
<?php
function check_utf8($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false;
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return false;
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return false;
$bytes--;
}
}
}
return true;
} // end of check_utf8
?>
可以试试上面我写的这个!