无法使用PHP DOM从表的列中打印值

问题描述 投票:2回答:1

我正在尝试使用DOM库打印此表:

enter image description here

我希望将每一列中的所有值分别打印出来,可能在一个数组中。到目前为止,这是我的代码:

  foreach ($descrDom->find(".tbl-fit") as $key => $td) { 
    $descr=$td->plaintext ."\n";
    //var_dump($descr);   
  }

至此,我的代码正在打印来自表中每个html元素的所有数据,包括表标题。

php dom
1个回答
0
投票

您的意思是这样

$column= 0; // The column number you want to be printed
$cell = 1;
while ($b = $descrDom->find("table.tbl-fit tr", $cell)) {
    $result[] = $b->children($column)->plaintext;
    $cell++;
}

print_r($result);

输出:

ALFA ROMEO MITO (955_)
ALFA ROMEO MITO (955_)
FIAT 500 (321_)
FIAT 500 (321_)
FIAT 500 C(321_)
.. etc

如果要获取类型列,只需将$column变量更改为1,依此类推。

© www.soinside.com 2019 - 2024. All rights reserved.