First thing you have to check the Content-type header.
//add error handling
$f = fopen($url, "r");
$md = stream_get_meta_data($f);
$wd = $md["wrapper_data"];
foreach($wd as $response) {
if (preg_match('/^content-type: .+?/.+?;\\s?charset=([^;"\\s]+|"[^;"]+")/i',
$response, $matches) {
$charset = $matches[1];
break;
}
}
$data = stream_get_contents($f);
You can then fallback on the meta
element. That's been answered before here.
More complex version of header parsing to please the audience:
if (preg_match('~^content-type: .+?/[^;]+?(.*)~i', $response, $matches)) {
if (preg_match_all('~;\\s?(?P<key>[^()<>@,;:\"/[\\]?={}\\s]+)'.
'=(?P<value>[^;"\\s]+|"[^;"]+")\\s*~i', $matches[1], $m)) {
for ($i = 0; $i < count($m['key']); $i++) {
if (strtolower($m['key'][$i]) == "charset") {
$charset = trim($m['value'][$i], '"');
}
}
}
}