簡體   English   中英

簡單的 html dom 將第一個孩子解析為一個數組

[英]simple html dom parsing first child into an array

考慮如下 html 代碼

<html>
<div class="allItems">
   <div class="Item" id="12345">
      <div class="ItemName">Tom</div>
      <div class="ItemAge">34</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="17892">
      <div class="ItemName">Dick</div>
      <div class="ItemAge">23</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="98776">
      <div class="ItemName">Harry</div>
      <div class="ItemAge">65</div>
      <div class="ItemGender">male</div>
   </div>
</div>
</html>

我試圖解析這個 html 文檔如下:

<?php

include_once("/simple_html_dom.php");

<--a bunch of code that works until here -->

$html = str_get_html($str); //this pulls the html code fine

//this is where my array constructions does not work
foreach ($html->find('div.allItems > *') as $article) {
   $item['name'] = $html->find('div.ItemName', 0)->plaintext;
   $item['age'] = $html->find('div.ItemAge',0)->plaintext;
   $item['gender'] = $html->('div.ItemGender', 0)->plaintext;
   $articles[] = $item;
}

print_r($articles);

?>

我希望得到什么:

Array ( [0] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[1] => Array ( [name] => Dick [Age] => 23 [Gender] => male ) 
[2] => Array ( [name] => Harry [Age] => 65 [Gender] => male )

相反,這是我得到的

Array ( [0] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[1] => Array ( [name] => Tom [Age] => 34 [Gender] => male ) 
[2] => Array ( [name] => Tom [Age] => 34 [Gender] => male )

因此,我的問題:

如何獲得所需的數組?

您走對了,唯一的想法是您需要將 div 的索引設置為find函數的第二個參數。 看看下面的解決方案:

include_once("simple_html_dom.php");
$str = '<html>
<div class="allItems">
   <div class="Item" id="12345">
      <div class="ItemName">Tom</div>
      <div class="ItemAge">34</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="17892">
      <div class="ItemName">Dick</div>
      <div class="ItemAge">23</div>
      <div class="ItemGender">male</div>
   </div>
   <div class="Item" id="98776">
      <div class="ItemName">Harry</div>
      <div class="ItemAge">65</div>
      <div class="ItemGender">male</div>
   </div>
</div>
</html>';

$html = str_get_html($str); //this pulls the html code fine

//this is where my array constructions does not work
$i = 0;
foreach ($html->find('div.allItems > *') as $article) {
    $item['name'] = $html->find('div.ItemName', $i)->plaintext;
    $item['age'] = $html->find('div.ItemAge',$i)->plaintext;
    $item['gender'] = $html->find('div.ItemGender', $i)->plaintext;
    $articles[] = $item;
    $i++;
}

print_r($articles);

輸出:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 34
            [gender] => male
        )

    [1] => Array
        (
            [name] => Dick
            [age] => 23
            [gender] => male
        )

    [2] => Array
        (
            [name] => Harry
            [age] => 65
            [gender] => male
        )

)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM