I am retrieving the nearest locations available from a given address (Longitude/Latitude) from geolocation website.
It works fine, but for some places it gives junk characters in the name. Moreover, in browser I am getting different characters compared to my PHP CURL functionality.
Here is the URL
One of the location is “Sitammapeta” in original location name, but in browser I am getting “Sītammapeta” where as in CURL function I am getting “SÄ«tammapeta”.
Please tell me why this difference. I wrote a function to convert browser output to original which works fine.
function convert ($old)
{
$n="";
for ($i=0; $i<strlen($old); $i++)
{
$n .= chr(ord(substr($old,$i,1)));
}
return $n;
}
But I dont understand how I convert the CURL output to original name.
EDIT
CURL code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
$response = curl_exec($ch);
curl_close ($ch);
The problem is that you’re displaying the data in your web-browser, that the data your sending is UTF-8 encoded and that your browser has no idea about that. If you add
<meta charset="utf-8" />or<meta http-equiv="Content-Type" content="text/html; charset=utf-8">to the head of your HTML document, that should solve your problem.Without the charset defined as UTF-8:
With
<meta charset="utf-8" />or<meta http-equiv="Content-Type" content="text/html; charset=utf-8">:Alternatively, you could have your PHP script send a content-type header using
header('Content-Type:text/html; charset=utf-8');(or replacetext/htmlwithtext/plainif you want to use plain text).Reference:
According to RFC2616 Section 3.7.1:
That means that if you don’t specify a charset, ISO-8859-1 will (should) be used, and as such, some characters won’t be displayed properly if the data is UTF-8 encoded.