字符的asc码操作

<!--提交一个字符串,对字符串中的每一个字符进行检查,如果是符号,则把符号转换为ASC的16进制形式
例如 http://127.0.0.1/test.php?a=hello'world
 
则应该输出为hello27world-->

<?php
$str=$_REQUEST["a"];
//$res=explode(" ",$str);
//substr($str,6,1);


for($i=0; $i<strlen($str); $i++)
{
$sstt=$sstt.substr($str,$i,1).","; //把字符串中的每个字符取出来,生成新的字符串
}
$ttyy=substr($sstt,0,strlen($sstt)-1); //把字符的最后一个“,”去掉
//echo $ttyy;

$res=explode(",",$ttyy); //把字符串分割为数组


for($i=0; $i<count($res); $i++)
{
if((ord($res[$i])<=47&&ord($res[$i])>=8)||(ord($res[$i])<=64&&ord($res[$i])>=58)||(ord($res[$i])<=96&&ord($res[$i])>=91)||(ord($res[$i])<=254&&ord($res[$i])>=123)){ //取得字符串的ASC码为特殊字符的时候
$string=$string.bin2hex($res[$i]); //bin2hex()这个函数可以把二进制的字符转化为16进制的字符
}
else{
$string=$string.$res[$i]; //如果不为特殊字符,直接输出
}
}
echo $string;
?>

 

posted on 2010-09-17 11:10  lovening  阅读(1494)  评论(0编辑  收藏  举报