【问题标题】:How to parse html table to array with symfony dom crawler如何使用 symfony dom 爬虫将 html 表解析为数组
【发布时间】:2016-10-30 03:47:49
【问题描述】:

我有 html 表,我想从该表中创建数组

$html = '<table>
<tr>
    <td>satu</td>
    <td>dua</td>
</tr>
<tr>
    <td>tiga</td>
    <td>empat</td>
</tr>
</table>

我的数组必须是这样的

array(
   array(
      "satu",
      "dua",
   ),
   array(
     "tiga",
     "empat",
   )
)

我已经尝试了下面的代码,但无法得到我需要的数组

$crawler = new Crawler();
$crawler->addHTMLContent($html);
$row = array();
$tr_elements = $crawler->filterXPath('//table/tr');
foreach ($tr_elements as $tr) {
 // ???????
}

【问题讨论】:

  • 您是否检查过此链接,其中包含完整的详细信息symfony.com/doc/current/components/dom_crawler.html
  • 是的。我有。,我只是无法理解爬虫在 foreach 中是如何工作的。,
  • 第一个代码块中的 HTML 缺少单引号。错字?
  • 不,这只是例子。

标签: php arrays symfony domcrawler


【解决方案1】:
$table = $crawler->filter('table')->filter('tr')->each(function ($tr, $i) {
    return $tr->filter('td')->each(function ($td, $i) {
        return trim($td->text());
    });
});

print_r($table);

上面的例子会给你一个多维数组,其中第一层是表格行“tr”,第二层是表格列“td”。

编辑

如果您有嵌套表,此代码会将它们很好地展平为一维数组。

$html = 'MY HTML HERE';
$crawler = new Crawler($html);

$flat = function(string $selector) use ($crawler) {
    $result = [];
    $crawler->filter($selector)->each(function ($table, $i) use (&$result) {
        $table->filter('tr')->each(function ($tr, $i) use (&$result) {
            $tr->filter('td')->each(function ($td, $i) use (&$result) {
                $html = trim($td->html());
                if (strpos($html, '<table') !== FALSE) return;

                $iterator = $td->getIterator()->getArrayCopy()[0];
                $address = $iterator->getNodePath();

                if (!empty($html)) $result[$address] = $html;
            });
        });
    });
    return $result;
};

// The selector gotta point to the most outwards table.
print_r($flat('#Prod fieldset div table'));

【讨论】:

  • 这好多了(y)
【解决方案2】:
$html = '<table>
            <tr>
                <td>satu</td>
                <td>dua</td>
            </tr>
            <tr>
                <td>tiga</td>
                <td>empat</td>
            </tr>
            </table>';

    $crawler = new Crawler();
    $crawler->addHTMLContent($html);
    $rows = array();
    $tr_elements = $crawler->filterXPath('//table/tr');
    // iterate over filter results
    foreach ($tr_elements as $i => $content) {
        $tds = array();
        // create crawler instance for result
        $crawler = new Crawler($content);
        //iterate again
        foreach ($crawler->filter('td') as $i => $node) {
           // extract the value
            $tds[] = $node->nodeValue;

        }
        $rows[] = $tds;

    }
    var_dump($rows );exit;

会显示

array 
  0 => 
    array 
      0 => string 'satu' 
      1 => string 'dua' 
  1 => 
    array (size=2)
      0 => string 'tiga' 
      1 => string 'empat'

【讨论】:

  • @wpacoder 没问题,需要补充说明吗?仅供参考,在 stackoverflow 中说谢谢并不是很好(cmets 不是为了那个),点赞就足够了
  • 没关系,我知道我的错在哪里。,
猜你喜欢
  • 1970-01-01
  • 2013-05-04
  • 2012-05-09
  • 2012-07-14
  • 2019-04-17
  • 2016-10-25
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
相关资源
最近更新 更多