1

我刚刚将 bing 地图嵌入到我的网站中,获取纬度和经度的查询字符串对于每个用户都是可变的。不幸的是,在我的国家和城市的免费数据库中,我有一些糟糕的符号,例如在这个位置Bouaké, Côte d’Ivoire,file_get_contents() 函数无法读取这些符号,因为它们变成了Bouaké,%20Côte%20d’Ivoire. 谁能告诉我如何逃避这些角色?实际上,我也很乐意删除它们或用他们的英语联想代替它们,例如é -> e. 先感谢您!

4

2 回答 2

2

Bouaké,%20Côte%20d’Ivoire字符串看起来已经被转义但是对于 html。您必须使用 html_entity_decode() 将它们转换回来,然后对于 url,有rawurlencode()将您的字符串放入低谷。

如果您可以在没有 html 实体的情况下获得输入,只需rawurlencode()在将它们添加到请求 URL 之前使用这些字符串。

更新

从您的评论看来,简单地按原样发送名称是行不通的。您可以尝试用非重音字母替换附加字母。为此,您需要在 php 环境和 iconv 中安装正确的语言环境(假设您的输入是 utf8):

$str = 'Bouaké,%20Côte%20d’Ivoire';
$old_locale = setlocale(LC_ALL, 'en_US.UTF8'); // setting the locale to an english one, saving the old
$ascii = iconv(
    'UTF-8',
    'ASCII//TRANSLIT//IGNORE', html_entity_decode($str, ENT_QUOTES, 'utf-8')
); // convert input to ascii transliterate from the locale data and ignore anything that cant be transliterated.
setlocale(LC_ALL, $old_locale); // restore the old locale
print rawurlencode($ascii); // => shoud print Bouake%2C%2520Cote%2520d%27Ivoire

这应该将您的字符串转换为您可以编码的无上升 ascii ('例如 -s )。

于 2013-06-10T09:01:39.217 回答
0

用于iconv()请求的字符编码。

$data = file_get_contents('http://www.example.com/');
iconv("UTF-8", "ISO-8859-1", $data);
于 2013-06-10T08:59:56.977 回答