简体   繁体   English

PHP中的动态URL在href标记中传递“&”时不起作用

[英]Dynamic URL in PHP not working when passing “&” in href tag

I have the following URL 我有以下网址

http://localhost:8777/business.php?id=Mobiles and Tablets 

When I am passing "and" in id it is redirecting me to the desired page but when I am passing 当我在id中传递“和”时,它会将我重定向到所需的页面,但是当我通过时

http://localhost:8777/business.php?id=Mobiles & Tablets 

& ie ampersand in id it is giving 404 error. &即id中的&符号给出404错误。

My PHP Code is like this: 我的PHP代码是这样的:

<a href="business.php?id=<? echo $cat;?>"><? echo $cat;?></a>

You have to use urlencode() in your php code. 你必须在你的PHP代码中使用urlencode()

<a href="business.php?id=<? echo urlencode($cat) ;?>"><? echo $cat;?></a>

It will generate link like: 它将生成如下链接:

http://localhost:8777/business.php?id=Mobiles+%26+Tablets

Space will be converted to + , and & will be converted to %26 Space将转换为+&将转换为%26

use php urlencode() 使用php urlencode()

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page. 编码要在URL的查询部分中使用的字符串时,此函数很方便,这是将变量传递到下一页的便捷方式。

And use urldecode() to decode your encoded argument 并使用urldecode()来解码您的编码参数

Decodes any %## encoding in the given string. 解码给定字符串中的任何%##编码。 Plus symbols ('+') are decoded to a space character. 加号('+')被解码为空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM