搜索引擎URL中的汉字解析

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

搜索引擎URL中的汉字解析

代码说明一切:

<? php
/*
 * Created on 2008-2-4
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 
*/
//  "http://www.baidu.com/baidu?wd=%D5%FD%D4%F2%B1%ED%B4%EF%CA%BD&q=3"

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

function  ConvertUrlDecode( $textUrlChar )
{
    
$textUrl   =   htmlspecialchars ( urldecode ( $textUrlChar ));
    
return   $textUrl ;
}
 
function  ConvertEncoding( $textEncode )
{
    
$textEncode   =   mb_convert_encoding( $textEncode , " UTF-8 " , " GB2312 " );
    
return   $textEncode ;
}


function  Chinese( $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 " ;
}
else   if  (  $radioflg   ==   " UTF-8 "  )
{
    
$radioUTF8   =   "  checked " ;
}
else
{
    
$radioUTF8   =   "  checked " ;
}

?>

<! DOCTYPE html  PUBLIC   " -//W3C//DTD XHTML 1.0 Transitional//EN "
 
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >  
< html xmlns = " http://www.w3.org/1999/xhtml "  xml : lang = " ja "  lang = " ja " >
< head >
< meta http - equiv = " Content-Language "  content = " ja "   />
< meta http - equiv = " Content-Script-Type "  content = " text/javascript "   />
< meta http - equiv = " Content-Type "  content = " text/html; charset=utf-8 " />  
< title > URL解析 </ title >
< script language = " Javascript " >
function  btSubmit()
{
    document
. urlrequest . action  =   ' urlcharacters.php ' ;
    document
. urlrequest . submit();
}
function  radioSubmit(flgType)
{
    document
. urlrequest . radioflg . value  =  flgType;
}
</ script >
</ head >
< body >
< form name = " urlrequest "  method = " post " >
< textarea name = " urltext "  rows = " 8 "  cols = " 40 " ><?= $_REQUEST [ " urltext " ]; ?></ textarea >
< p >

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

< input type = " radio "  name = " mainrd "  id = " UTF-8 "   onclick = " javascript:radioSubmit('UTF-8') "   value = " 1 "   <?=   $radioUTF8   ?>>
< label id = " label_UTF-8 "    for = " UTF-8 "  style = " CURSOR:hand " > UTF - 8 </ label >
    
</ p >
< p >< input type = " 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> " ;

echo  Chinese( $convertText );

?>
</ p >

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值