php中urldecode()和urlencode()起什么作用?

urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%。

urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其原理就是把十六进制字符串转换为中文字符!
urlencode()编码:对字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。
urldecode()解码:还原 URL 编码字符串。
示例:
<?php
header("Content-Type:text/html; charset=utf-8");
//对参数值进行编码
$parm=urlencode("演示php-mysql");

//拼接url
$url="decode.php?par=".$parm;
?>

<a href="<?php echo $url;?>">urlencode演示</a>

点击连接后地址栏中汉字被编码了:
http://localhost/decode.php?par=%E6%BC%94%E7%A4%BAphp-mysql
------------------------------------------------------------------------

//decode.php
<?php
//获取参数值
$parValue=$_GET['par'];

//解码
echo urldecode($parValue);
//运行结果:演示php-mysql
?>

猜你喜欢

转载自blog.csdn.net/Aaroun/article/details/80859247