搜索引擎URL中的汉字解析

本文介绍了一种用于解析搜索引擎URL中汉字编码的方法,并提供了一个PHP脚本实例。该脚本能够从URL中提取汉字编码,进行URL解码,并将编码转换为指定格式。此外,还包含了一个用于判断汉字编码类型的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript"><!-- google_ad_client = "pub-0241434510974184"; /* auto-http.cn 右边 ,468x60 */ google_ad_slot = "0902256228"; google_ad_width = 468; google_ad_height = 60; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

搜索引擎URL中的汉字解析

代码说明一切:

<?php
/*
*Createdon2008-2-4
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window-Preferences-PHPeclipse-PHP-CodeTemplates
*/
//"http://www.baidu.com/baidu?wd=%D5%FD%D4%F2%B1%ED%B4%EF%CA%BD&q=3"

functionrequestCharacters($strURL)
{
if(preg_match_all("/(%[a-zA-Z0-9]{2})+/",$strURL,$matches)!=FALSE)
{
return$matches[0][0];
}
return"";
}

functionConvertUrlDecode($textUrlChar)
{
$textUrl=htmlspecialchars(urldecode($textUrlChar));
return$textUrl;
}

functionConvertEncoding($textEncode)
{
$textEncode=mb_convert_encoding($textEncode,"UTF-8","GB2312");
return$textEncode;
}


functionChinese($word="")
{
if(empty($word))
{
return"";
}
if(preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word)==true||preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word)==true||preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)==true)
{
return"结果:很好,这是一个UTF-8编码的汉字";
}
else
{
return"结果:抱歉,这不是一个UTF-8编码的汉字";
}
}
$radioGB2312="";
$radioUTF8="";
$radioflg=$_REQUEST['radioflg'];
if($radioflg=="GB2312")
{
$radioGB2312="checked";
}
elseif($radioflg=="UTF-8")
{
$radioUTF8="checked";
}
else
{
$radioUTF8="checked";
}

?>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="ja"lang="ja">
<head>
<metahttp-equiv="Content-Language"content="ja"/>
<metahttp-equiv="Content-Script-Type"content="text/javascript"/>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>URL解析</title>
<scriptlanguage="Javascript">
functionbtSubmit()
{
document
.urlrequest.action='urlcharacters.php';
document
.urlrequest.submit();
}
functionradioSubmit(flgType)
{
document
.urlrequest.radioflg.value=flgType;
}
</script>
</head>
<body>
<formname="urlrequest"method="post">
<textareaname="urltext"rows="8"cols="40"><?=$_REQUEST["urltext"];?></textarea>
<p>

<inputtype="radio"name="mainrd"id="GB2312"onclick="javascript:radioSubmit('GB2312')"value="0"<?=$radioGB2312?>>
<labelid="label_GB2312"for="GB2312"style="CURSOR:hand">GB2312</label>

<inputtype="radio"name="mainrd"id="UTF-8"onclick="javascript:radioSubmit('UTF-8')"value="1"<?=$radioUTF8?>>
<labelid="label_UTF-8"for="UTF-8"style="CURSOR:hand">UTF-8</label>

</p>
<p><inputtype="button"value="URL汉字编码解析"onclick="javascript:btSubmit();"/></p>
<p>
<?php
$strURL=$_REQUEST["urltext"];//"http://www.baidu.com/baidu?wd=%D5%FD%D4%F2%B1%ED%B4%EF%CA%BD&q=3";

$textEncode=requestCharacters($strURL);


echo"正则表达式提取:".$textEncode;

$convertText=ConvertUrlDecode($textEncode);


if($radioflg=="GB2312")
{
$convertText=ConvertEncoding($convertText);
}


echo"<br><br>URL解析:$convertText<br><br>";

echoChinese($convertText);

?>
</p>

<inputtype="hidden"name="radioflg"id="radioflg"value="<?=$radioflg?>"/>
</form>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值