Comment parse et process HTML/XML en PHP

Mots clés : phphtmlxmlxml-parsinghtml-parsingphp

meilleur 3 Réponses Comment parse et process HTML/XML en PHP

vote vote

95

// Create DOM from URL or file $html = file_get_html('http://www.example.com/');  // Find all images foreach($html->find('img') as $element)        echo $element->src . '<br>';  // Find all links foreach($html->find('a') as $element)        echo $element->href . '<br>'; 
// Create DOM from string $html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');  $html->find('div', 1)->class = 'bar';  $html->find('div[id=hello]', 0)->innertext = 'foo';  echo $html; 
// Dump contents (without tags) from HTML echo file_get_html('http://www.google.com/')->plaintext; 
// Create DOM from URL $html = file_get_html('http://slashdot.org/');  // Find all article blocks foreach($html->find('div.article') as $article) {     $item['title']     = $article->find('div.title', 0)->plaintext;     $item['intro']    = $article->find('div.intro', 0)->plaintext;     $item['details'] = $article->find('div.details', 0)->plaintext;     $articles[] = $item; }  print_r($articles); 
vote vote

84

<a\s+class="?playbutton\d?[^>]+id="(\d+)".+?    <a\s+class="[\w\s]*title [\w\s]*"[^>]+href="(http://[^">]+)"[^>]*>([^<>]+)</a>.+? 
$div->find(".stationcool a")->attr("title"); 
vote vote

71

 $qp = qp("<html><body><h1>title</h1>..."); // or give filename or URL 
 $qp->find("div.classname")->children()->...;   foreach ($qp->find("p img") as $img) {      print qp($img)->attr("src");  } 
 $qp->xpath("//div/p[1]");  // get first paragraph in a div 
 $qp->find("a[target=_blank]")->toggleClass("usability-blunder"); 

Questions similaires