I am loading contents from a URL; the URL is in the form http://www.example.com/?keyword=something. I get the specific content based on user’s keyword like this:
$url = 'www.example.com/?';
$url = $url."keyword=$something";
function getData ($url) {
$data = file_get_contents($url);
return $data;
}
The original data contains Scandinavian characters like Ö or Å. After loading, those characters are not any more readable. How to fix this special character problem?
UPDATE:
I changed the code this way:
function getData ($url) {
$data = urlencode(file_get_contents($url));
$data = urldecode($data);
return $data;
}
Didn’t help either. Also $data = utf8_decode(urldecode($data)); and echo utf8_decode(urldecode(getData($keyword))); don’t help. What am I doing whrong here?
file_get_contentsis not charset aware. It returns the exact bytes that it is served up. This means that if the url returns UTF-8, and you display it as iso-8859-1, then things will look wrong. Most likely, this is the case (But it could be the other way around). Either convert to ISO-8859-1, or change your app to use UTF-8. The former is possibly the simplest change – Pipe the content throughutf8_decode, which transforms from UTF-8 to ISO-8859-1.E.g.: