1

I'm working on this code that when I try to execute it on http://www.secra.de don't works but if I use it on http://www.elektro-guttau.de/ it show the correct value, ISO-8859-1 I think the if(!empty()) is not working. Some1 know how do it works?

$url = "http://www.secra.de/";
$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($html);
$items = $doc->getElementsByTagName("meta");
if ($items->length > 0) {
    $info = $items->item(0)->getAttribute('content');
    if (!empty($info)) {
        $info = $items->item(0)->getAttribute('charset');
    }
    echo $info;

    $array = array($info);
    $split = explode('=', $info);

    $encode = end($split);
    echo $encode;
}
Anthy
  • 60
  • 1
  • 8

1 Answers1

1

If you change your code to not pick up the errors and to also look at all of the meta tags (in the lines using $items->item(0) you are only looking at the first one)...

$url = "http://www.secra.de/";
$html = file_get_contents($url);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);

$items = $doc->getElementsByTagName("meta");
if ($items->length > 0) {
    $info = "";
    foreach ( $items as $item ) {
        if ($item->hasAttribute('charset')) {
            $info = $item->getAttribute('charset');
        }
        elseif ($item->hasAttribute('content') && $item->hasAttribute('http-equiv')) {
            $info = $item->getAttribute('content');
            $parts = explode("=", $info);
            $info = end($parts);
        }
    }
    echo $info.PHP_EOL;

}

This code looks through the meta tags and if there is a charset attribute, then it stores it and echos it out at the end.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • can you explainme $info.PHP_EOL; please? – Anthy Jun 08 '18 at 13:52
  • All that is doing is displaying `$info` - PHP_EOL is a way of putting a new line on the end of a string (only here for debugging purposes) – Nigel Ren Jun 08 '18 at 13:53
  • Hello mate, sorry for ask another time but www.yahoo.com is not showing "UTF-8" :C – Anthy Jun 08 '18 at 14:24
  • I've had a quick look and yahoo.com doesn't give you a proper HTML page. If you add `echo $doc->saveHTML();` it seems to be a dummy page. This may be to stop people accessing it this way (i.e. not via a browser). – Nigel Ren Jun 08 '18 at 14:27
  • https://stackoverflow.com/questions/16624919/why-iframe-dosent-work-for-yahoo-com may give some further info. – Nigel Ren Jun 08 '18 at 14:28
  • oh ok, so there aren't solution for this? – Anthy Jun 08 '18 at 14:29
  • Not easy at all, they seem to do a bit to stop you getting there content. – Nigel Ren Jun 08 '18 at 14:32