Question

J'utilise simple_html_dom pour analyser un site Web. Est-il possible d'extraire le doctype?

Était-ce utile?

La solution

Vous pouvez utiliser la fonction file_get_contents pour obtenir toutes les données HTML du site Web. Par exemple

<?php
   $html = file_get_contents("http://google.com");
   $html = str_replace("\n","",$html);
   $get_doctype = preg_match_all("/(<!DOCTYPE.+\">)<html/i",$html,$matches);
   $doctype = $matches[1][0];
?>

Autres conseils

Vous pouvez utiliser $html->find('unknown'). Cela fonctionne - au moins - dans la version 1.11 de la bibliothèque simplehtmldom. Je l'utilise comme suit:

function get_doctype($doc)
{
    $els = $doc->find('unknown');

    foreach ($els as $e => $el) 
        if ($el->parent()->tag == 'root') 
            return $el;

    return NULL;
}

C'est juste pour gérer tous les autres éléments « inconnus » qui pourraient se trouver; Je suppose le premier sera le doctype. Vous pouvez explicitement inspecter ->innertext si vous voulez vous assurer qu'il commence par '!DOCTYPE ', cependant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top