PHP / MySQL - URL中的特殊字符。如何避免? [英] PHP/MySQL - Special characters in URLs. How to avoid?
问题描述
我的数据库包含从外部Feed提取的信息。在此原始文字Feed中,以下文字用于替换特殊字符:
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;
é - é
我提取一些文本以形成URL。例如,我从包含这些字符的数据构造的URL可能如下所示:
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
我使用此URL中的GET变量来构造进一步的查找,这导致一些具体问题:
I use the GET variables in this URL to construct further lookups, which leads to a couple of specific problems:
-
é和 strong>字符会在显示时发送回MySQL,因此它们不会触发任何结果(因为字符在数据库文本中占用完整的HTML表单)。
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).
网址中的& 会分隔变量,只有富兰克林才会返回整个字符串。
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?
>
推荐答案
如果你使用php生成链接,你应该在创建链接之前编码品牌= Franklin&Hédgsonuse urlencode http://php.net/manual/en/function.urlencode.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
然后,您可以使用htmlentities对查询进行编码,然后将其发送到查询中的mysql http://www.php.net/manual/en/function.htmlentities.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
这篇关于PHP / MySQL - URL中的特殊字符。如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!