Home > Backend Development > PHP Tutorial > PHP 中文处理技巧_PHP

PHP 中文处理技巧_PHP

WBOY
Release: 2016-06-01 12:19:56
Original
783 people have browsed it

折腾了一天的时间,才弄出点眉目来。
做AJAX应用或Flash应用,提交中文内容到后台,涉及到编码解码(encode、decode)及编码格式的转换。
网上的PHP端escape unescape函数建议不要用,它把中英文混合时的英文过滤掉了,我是莫名其妙了N久啊,建议用unicode_urldecode这个。
再就是编码格式的转换,这主要涉及数据的存储和客户端返回,用iconv就搞定,这个函数似乎是从C++中借鉴来的。
复制代码 代码如下:
function unicode_urldecode($url)
{
preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
foreach ($a[1] as $uniord)
{
$dec = hexdec($uniord);
$utf = '';
if ($dec {
$utf = chr($dec);
}
else if ($dec {
$utf = chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
else
{
$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
$url = str_replace('%u'.$uniord, $utf, $url);
}
return urldecode($url);
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template