[英]Dynamic URL in PHP not working when passing “&” in href tag
我有以下网址
http://localhost:8777/business.php?id=Mobiles and Tablets
当我在id中传递“和”时,它会将我重定向到所需的页面,但是当我通过时
http://localhost:8777/business.php?id=Mobiles & Tablets
&即id中的&符号给出404错误。
我的PHP代码是这样的:
<a href="business.php?id=<? echo $cat;?>"><? echo $cat;?></a>
你必须在你的PHP代码中使用urlencode()
。
<a href="business.php?id=<? echo urlencode($cat) ;?>"><? echo $cat;?></a>
它将生成如下链接:
http://localhost:8777/business.php?id=Mobiles+%26+Tablets
Space
将转换为+
, &
将转换为%26
使用php urlencode()
编码要在URL的查询部分中使用的字符串时,此函数很方便,这是将变量传递到下一页的便捷方式。
并使用urldecode()来解码您的编码参数
解码给定字符串中的任何%##编码。 加号('+')被解码为空格字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.