<?php
if (isset($_GET['q']) and $_GET['q']!=""){
$q_url = $_GET['q'];
}else{
$q_url = "小米2";
}
function charsetToUTF8($mixed)
{
if (is_array($mixed)) {
foreach ($mixed as $k => $v) {
if (is_array($v)) {
$mixed[$k] = charsetToUTF8($v);
} else {
$encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
if ($encode == 'EUC-CN') {
$mixed[$k] = iconv('GBK', 'UTF-8', $v);
}
}
}
} else {
$encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
if ($encode == 'EUC-CN') {
$mixed = iconv('GBK', 'UTF-8', $mixed);
}
}
return $mixed;
}
$str = charsetToUTF8($q_url);
//strlen() 函数返回字符串的长度
//dechex() 函数把十进制转换为十六进制
//strtoupper() 函数把字符串转换为大写
//ord() 函数返回字符串第一个字符的 ASCII 值
$ret = '';
for($i=0, $len=strlen($str);$i < $len; $i++) {
$ret .= '%'.strtoupper(dechex(ord($str[$i])));
}
echo "<br>";
echo "UTF结果:<br>".$ret;
echo "<br>";
echo "过滤%结果:<br>".strtolower(str_replace("%","",$ret));
echo "<br>";
$s = strtolower(str_replace("%","",$ret));
$s2=strtoupper(implode(str_split($s,2),"%"));
echo "增加%号的结果:<br>%".$s2;
echo "<br>";
echo "转为中文结果:<br>".mb_convert_encoding(urldecode("%".$s2),'gb2312','utf-8');
UTF结果:
%E5%B0%8F%E7%B1%B3%32
过滤%结果:
e5b08fe7b1b332
增加%号的结果:
%E5%B0%8F%E7%B1%B3%32
转为中文结果:
小米2