PHP/MySQL - URL 中的特殊字符。如何避免?

发布于 2024-08-27 13:23:08 字数 585 浏览 4 评论 0原文

我的数据库包含从外部提要中提取的信息。在此原始文本提要中,使用以下文本代替特殊字符:

& - &
' - &39;
é - é

我提取了其中的一些文本以形成 URL。例如,我根据包含这些字符的数据构建的 URL 可能如下所示:

http://url.com/search/?brand=Franklin&Hédgson's

我使用此 URL 中的 GET 变量来构建进一步的查找,这会导致一些特定问题:

  1. é 和 ' 字符在出现时被发送回 MySQL,因此它们不会触发任何结果(因为这些字符在数据库文本中采用完整的 HTML 形式)。

  2. URL 中的 & 分隔变量,GET 仅返回 Franklin,而它应该返回整个字符串。

有没有直接的方法来处理这个问题?

谢谢。

My database contains information extracted from an external feed. In this raw text feed, the following text is used in place of special characters:

& - &
' - &39;
é - é

I extract some of this text to form URLs. For example, a URL that I construct from data containing these characters might look like this:

http://url.com/search/?brand=Franklin&Hédgson's

I use the GET variables in this URL to construct further lookups, which leads to a couple of specific problems:

  1. The é and ' characters are sent back to MySQL as they appear, and so they don't trigger any results (because the characters take the full HTML form in the database text).

  2. The & within the URL separates the variable, and the GET returns only Franklin, when it should return the whole string.

Are there any straightforward ways of dealing with this?

Thanks.

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

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

发布评论

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

评论(2

谁与争疯 2024-09-03 13:23:08

如果您使用 php 生成链接,则应在创建链接之前对 Brand=Franklin&Hédgson's 进行编码,使用 urlencode http://php.net/manual/en/function.urlencode.php

然后,您可以使用 htmlentities 对查询进行编码,然后再将查询发送到 mysql http://www.php.net/manual/en/function.htmlentities.php

You should encode the brand=Franklin&Hédgson's before creating the link, if you are generating the link with php use urlencode http://php.net/manual/en/function.urlencode.php

Then you can use htmlentities to encode the query before sending it off to mysql in a query http://www.php.net/manual/en/function.htmlentities.php

指尖微凉心微凉 2024-09-03 13:23:08

那是因为一个“&”在 URL 中启动一个新的 GET 变量。这 '?'以包含 2 个项目的查询字符串开头,“brand”=“Franklin”和 undefined =“Hédgson's”。

这是URL 转义代码列表,每当出现这些情况时您都应该使用它字符出现在您的 URL 中(当然是在值内)。所以,“&”当放入字符串时需要转义为“%26”,以便 GET 能够正确读取它。

That's because a '&' within a URL starts a new GET variable. The '?' starts your query string with has 2 items in it, 'brand' = "Franklin" and undefined = "Hédgson's".

Here is a list of URL escape codes that you should use whenever any of those characters appear in your URL (inside a value, of course). So, the '&' needs to be escaped to '%26' when it is put into the string so that the GET will read it properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文