2014-09-23 47 views
1

在我目前的項目中,使用XPath我解析HTML代碼:XPath數據爆炸基於新線

<div class="prod_view_info"> 
    <p>notimportant</p><p>notimportant</p> 
    <p> 
     <b>Putere (Consum/ora): </b> 3W<br> 
     <b>Fasung: </b> MR16<br> 
     <b>Tensiune de alimentare: </b> 12V<br> 
     <b>Flux luminos: </b> 270 - 300 lm<br> 
     <b>Flux luminos per Watt: </b> 90-100 lm/W<br> 
     <b>Putere LED: </b> 3* 1W<br> 
     <b>Frecventa de lucru: </b> DC<br> 
     <b>Culoarea luminii: </b>alba calda<br> 
     <b>Temperatura: </b> 30/50°C<br> 
     <b>Material: </b> Aluminiu<br> 
     <b>Grad de protectie: </b> IP21<br> 
     <b>Durata de viata: </b> &gt; 50000 ore<br> 
     <b>Garantie: </b>2 ani<br> 
     <b>Certificate: </b> 
    </p> 
</div> 

與下面的PHP代碼:

foreach($xpathprd->query('//div[@class="prod_view_info"]/p[3]/node()') as $techdata) {  

    $techdatap = $techdata->nodeValue; 
    $techdatapChunks = explode(":", trim($techdatap)); 
    $producttechdata[] = $techdatapChunks; 
} 

$techdata_json = json_encode($producttechdata); 

echo "<hr>$techdata_json"; 

的一點是讓的對信息在每一行和使用JSON序列化,例如:

<b>Putere (Consum/ora): </b> 3W<br> 

應該是:

["Putere (Consum/ora)","3W"] 

但xpath去掉標籤和某處我失去了一些空間和爆炸的結果都搞砸了。

我該如何做這項工作?

回答

1

在這種情況下,您可以使用->childNodes->nextSibling的某種組合,以便更容易檢測。例如:Sample Demo

$dom = new DOMDocument(); 
$dom->loadHTML($html_string); 
$xpathprd = new DOMXPath($dom); 

$data = array(); 
$elements = $xpathprd->query('//div[@class="prod_view_info"]/p[3]')->item(0); 
foreach($elements->childNodes as $child) { 
    if(isset($child->tagName) && $child->tagName == 'b') { // check if its a `<b>` tag 
     $key = trim($child->nodeValue); 
     $value = trim($child->nextSibling->nodeValue); // get the next sibling which is a domtext 
     $data[] = array($key, $value); // push it inside 
    } 

} 

echo '<pre>'; 
$data = json_encode($data); 
print_r($data); 

應該產生:

[["Putere (Consum\/ora):","3W"],["Fasung:","MR16"],["Tensiune de alimentare:","12V"],["Flux luminos:","270 - 300 lm"],["Flux luminos per Watt:","90-100 lm\/W"],["Putere LED:","3* 1W"],["Frecventa de lucru:","DC"],["Culoarea luminii:","alba calda"],["Temperatura:","30 \/ 50\u00c2\u00b0C"],["Material:","Aluminiu"],["Grad de protectie:","IP21"],["Durata de viata:","> 50000 ore"],["Garantie:","2 ani"],["Certificate:",""]] 
+0

@LiviuZeJah確保人高興這有助於 – Ghost 2014-09-24 00:41:46