如何在 PHP 中仅对 URL 中的 id 进行编码? [已关闭]

问题描述 投票:0回答:1
<a class='btn btn-success' href='../posts/postview.php?id=".urlencode($row['category_id'])."'role='button'>See Posts</a></td>

这是我的按钮 url,但此 urlencode 方法不会返回任何编码的 id。相反,它会以数字形式返回完全相同的 id。我该怎么办???

我已经尝试过这个。但还没有得到任何结果。我期望 URL 中的数字 id 应该被编码。

php url urlencode
1个回答
2
投票

在 PHP 中的 URL 编码上下文中,重要的是要澄清您不需要对 URL 进行字母数字字符(包括数字)编码。 PHP的

urlencode
函数根据官方文档进行特定编码:

返回一个字符串,其中包含除 -_ 之外的所有非字母数字字符。已替换为百分号 (%) 后跟两个十六进制数字和编码为加号 (+) 的空格。它的编码方式与 WWW 表单中发布的数据的编码方式相同,即与 application/x-www-form-urlencoded 媒体类型中的方式相同。这与 » RFC 3986 编码(请参阅 rawurlencode())不同,因为由于历史原因,空格被编码为加号 (+)。

在您的场景中,无需手动编码字母数字字符。所以下面的例子应该适合你:

<a class='btn btn-success' href='../posts/postview.php?id=".$row['category_id']."' role='button'>See Posts</a></td>
© www.soinside.com 2019 - 2024. All rights reserved.