I have just embedded bing maps into my site and the query string to get latitude and longtitude is variable to each user. Unfortunately in my free database of countries and cities I have crappy symbols like for example in this location Bouaké, Côte d’Ivoire
which cannot be read by the file_get_contents() function because they turn into Bouaké,%20Côte%20d’Ivoire
. Can anyone tell me how to escape these characters? Actually I'd be happy with removing them too or replacing them with their english associatives like é -> e
. Thank you in advance!

- 15,199
- 21
- 74
- 144
2 Answers
The Bouaké,%20Côte%20d’Ivoire
string looks like it's already been escaped but for html. You will have to convert those back with html_entity_decode() and then for urls, there's rawurlencode() to put your strings trough.
If you can get to your input without the html entities, just use rawurlencode()
on these strings before you add them to your request url.
Update
It seems like from your comments that simply sending the name as is, won't work. You can try to replace the accended letters with non accented ones. For this you will need a proper locale installed in your php environment and iconv (assuming your input is in 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
This should convert your string to an asccent free ascii one that you can encode (for the '
-s for example).

- 1
- 1

- 20,425
- 6
- 51
- 54
-
That looks good but is still not accepted by file_get_contents(). I don't understand ?? Here's the error `Warning: file_get_contents(http://dev.virtualearth.net/REST/v1/Locations/Bouak%26eacute%3B%2C%2520C%26ocirc%3Bte%2520d%26rsquo%3BIvoire?key=xx): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request ` And even if it gets accepted I doubt that bing will return any results containing %26eacute and such :/ – php_nub_qq Jun 10 '13 at 09:07
-
The string "Bouak%26eacute%3B%2C%2520C%26ocirc%3Bte%2520d%26rsquo%3BIvoire" now seem to be a `rawurlencode()`d one with the html entities in side of them. – complex857 Jun 10 '13 at 09:58
-
Running `rawurlencode(html_entity_decode("Bouaké,%20Côte%20d’Ivoire", ENT_QUOTES, "utf-8"))` should result in: `Bouak%C3%A9%2C%2520C%C3%B4te%2520d%E2%80%99Ivoire` (I don't have a key at hand so can't test it right away) – complex857 Jun 10 '13 at 10:12
-
It doesn't find anything. The only workaround I could think of was to remove those letters and hope for the best. – php_nub_qq Jun 10 '13 at 10:20
-
prints `Bouak%27e%2C%20C%5Eote%20d%27Ivoire` : – php_nub_qq Jun 10 '13 at 11:23
-
I found [this](http://stackoverflow.com/a/12807569/2415293) function which seems a little heavy for a case like this but does the job perfectly! – php_nub_qq Jun 10 '13 at 11:31
-
Actually I just ran that to remove all accents from the database and now I have nothing to worry about! :P Thanks again! – php_nub_qq Jun 10 '13 at 11:40
use iconv()
for requested character encoding.
$data = file_get_contents('http://www.example.com/');
iconv("UTF-8", "ISO-8859-1", $data);

- 26,318
- 22
- 94
- 126
-
the problem is that the string in question is a parameter of the url like in this example: `Warning: file_get_contents(http://dev.virtualearth.net/REST/v1/Locations/Bouaké,%20Côte%20d’Ivoire?key=xx): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request` – php_nub_qq Jun 10 '13 at 09:05