2013-08-21 8 views
5

Ich weiß, wir können PHP DOM verwenden, um HTML mit PHP zu analysieren. Ich habe hier auch viele Fragen zum Stackoverflow. Aber ich habe eine spezifische Anforderung. Ich habe eine HTML-Inhalte wie unterWie HTML in PHP analysieren?

<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 1</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 1</span> 
</p> 
<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 2</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 2</span> 
</p> 
<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 3</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 3</span> 
</p> 

Ich möchte die oben HTML analysieren und die conent in zwei unterschiedliche Array wie

$heading und $content

$heading = array('Chapter 1','Chapter 2','Chapter 3'); 
$content = array('This is chapter 1','This is chapter 2','This is chapter 3'); 

speichere ich dies erreichen können einfach mit jQuery. Aber ich bin mir nicht sicher, ist es der richtige Weg. Es wäre großartig, wenn einige mich in die richtige Richtung zeigen könnten. Vielen Dank im Voraus.

+0

Verwendung jquery als seine Struktur einfach ist. – Notepad

+0

@Susheel: HTML-Inhalt wird viel größer sein, da es die Ausgabe nach dem Parsen von 'docx' Dateien ist – laradev

+0

Sie könnten reguläre Ausdrücke verwenden, wenn Sie nicht für PHP DOM gehen möchten. –

Antwort

5

Versuchen bei PHP Simple HTML DOM Parser

schauen Sie brillante Syntax ähnlich wie jQuery hat, so können Sie leicht jedes Element wählen Sie durch ID oder Klasse

8

ich verwendet habe DOMDocument und DOMXPath die Lösung zu erhalten, können Sie es finden Sie unter:

<?php 
$dom = new DomDocument(); 
$test='<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 1</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 1</span> 
</p> 
<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 2</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 2</span> 
</p> 
<p class="Heading1-P"> 
    <span class="Heading1-H">Chapter 3</span> 
</p> 
<p class="Normal-P"> 
    <span class="Normal-H">This is chapter 3</span> 
</p>'; 

$dom->loadHTML($test); 
$xpath = new DOMXpath($dom); 
    $heading=parseToArray($xpath,'Heading1-H'); 
    $content=parseToArray($xpath,'Normal-H'); 

var_dump($heading); 
echo "<br/>"; 
var_dump($content); 
echo "<br/>"; 

function parseToArray($xpath,$class) 
{ 
    $xpathquery="//span[@class='".$class."']"; 
    $elements = $xpath->query($xpathquery); 

    if (!is_null($elements)) { 
     $resultarray=array(); 
     foreach ($elements as $element) { 
      $nodes = $element->childNodes; 
      foreach ($nodes as $node) { 
       $resultarray[] = $node->nodeValue; 
      } 
     } 
     return $resultarray; 
    } 
} 

Live-Ergebnis:http://saji89.codepad.org/2TyOAibZ

+0

Der Code wurde für das richtige Ergebnis aktualisiert. – saji89

-2

// DOM Erstellen von URL oder Datei

$html = file_get_html('http://www.google.com/'); 

// Alle Bilder finden

foreach($html->find('img') as $element) 
    echo $element->src . '<br>'; 

// Suche alle Links

foreach($html->find('a') as $element) 
    echo $element->href . '<br>'; 
+0

file_get_html ?? Ist das eine PHP-Funktion? – everydayapps

+0

file_get_content ist richtig. er hat die kopie von php simple dom webseite zurück –