回显具有 ' 的变量在他们之中

发布于 2024-11-19 22:32:18 字数 268 浏览 1 评论 0原文

我有这样的回声,

echo "<a href = 'showdetails.php?name='$name'&type=movie'> </a>";

这正在按预期工作,但例如

$name = Gulliver's Travels;

,我只会在另一页中收到格列佛。

有什么办法可以绕过这个吗?

谢谢,

禅师。

I have this echo,

echo "<a href = 'showdetails.php?name='$name'&type=movie'> </a>";

that is working as intend, but if

$name = Gulliver's Travels;

for example, I will only receive Gulliver in the other page.

Is there any way of circumventing this?

Thanks,

Zenshi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-11-26 22:32:18

有许多字符不属于 URL,因此请对它们进行正确编码:

echo '<a href="showdetails.php?name=' . urlencode($name) . '&type=movie"> </a>';

href 值应括在双引号中,而不是单引号中,因为 URL 中允许使用单引号,但不允许使用双引号't。

http://php.net/manual/en/function.urlencode.php

如果您将其放入除链接之外的任何其他类型的 HTML 输出中,您需要使用 htmlspecialchars

There are many characters that don't belong in URLs, so encode them all properly:

echo '<a href="showdetails.php?name=' . urlencode($name) . '&type=movie"> </a>';

The href value should be enclosed in double quotes, not single quotes, since single quotes are allowed in URLs but double quotes aren't.

http://php.net/manual/en/function.urlencode.php

If you were putting this in any other type of HTML output than a link, you'd want to use htmlspecialchars.

深海夜未眠 2024-11-26 22:32:18

我会选择 urlencode 因为你将它用作 URL 的一部分:

echo "<a href = 'showdetails.php?name=".urlencode($name)."&type=movie'> </a>";

I'd go for urlencode because you are using it as part of an URL:

echo "<a href = 'showdetails.php?name=".urlencode($name)."&type=movie'> </a>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文