簡體   English   中英

PHP中的動態URL在href標記中傳遞“&”時不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM