本文实例讲述了php自定义函数实现汉字转换utf8编码的方法。分享给大家供大家参考,具体如下:
该函数有两部分组成:
function htou($c) {
$n = (ord($c[0]) & 0x1f) << 12;
$n += (ord($c[1]) & 0x3f) << 6;
$n += ord($c[2]) & 0x3f;
return $n;
}
//在代码中隐藏utf8格式的字符串
function utf8_unicode($str) {
$encode='';
for($i=0;$i<strlen($str);$i++) {
if(ord(substr($str,$i,1))> 0xa0) {
$encode.=''.htou(substr($str,$i,3)).';';
$i+=2;
} else {
$encode.=''.ord($str[$i]).';';
}
}
return $encode;
}
我们可以用 utf8_unicode() 就可以转换汉字,返回为汉字的utf8编码。这样输出网页不会是乱码。
[b]PS:这里再为大家推荐几款非常实用的在线编码转换工具供大家参考使用:[/b]
[b]UTF-8编码转换工具:
[/b][url=http://tools.jb51.net/transcoding/convutf]http://tools.jb51.net/transcoding/convutf[/url]
[b]在线编码转换工具(utf-8/utf-32/Punycode/Base64):
[/b][url=http://tools.jb51.net/transcoding/decode_encode_tool]http://tools.jb51.net/transcoding/decode_encode_tool[/url]
[b]Native/UTF-8在线编码转换工具:
[/b][url=http://tools.jb51.net/transcoding/native2utf8]http://tools.jb51.net/transcoding/native2utf8[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/459.htm]PHP编码与转码操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。