不能使用PHP DOM打印表格列中的数值。

huangapple go评论111阅读模式
英文:

Cannot print values from a table` columns using PHP DOM

问题

我正在尝试使用DOM库打印这个表格:

不能使用PHP DOM打印表格列中的数值。

我希望从每一列中获取所有的值,可能存储在一个数组中。以下是迄今为止我的代码:

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

目前,我的代码正在打印表格中每个HTML元素的所有数据,包括表头。

英文:

I am trying to print this table using the DOM library:

不能使用PHP DOM打印表格列中的数值。

I want all the values from each column to be printed separately, probably in an array. Here is my code so far:

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

At this point my code is printing all the data from each html element in the table including the table headers.

答案1

得分: 0

以下是您要的翻译部分:

  1. 这样的意思是
  2. $column= 0; // 您想要打印的列号
  3. $cell = 1;
  4. while ($b = $descrDom->find("table.tbl-fit tr", $cell)) {
  5. $result[] = $b->children($column)->plaintext;
  6. $cell++;
  7. }
  8. print_r($result);
  9. **输出:**
  10. ALFA ROMEO MITO (955_)
  11. ALFA ROMEO MITO (955_)
  12. FIAT 500 (321_)
  13. FIAT 500 (321_)
  14. FIAT 500 C(321_)
  15. .. 等等
  16. 如果您想获取类型列,只需将`$column`变量更改为1,以此类推。
英文:

You mean like this

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

Output:

  1. ALFA ROMEO MITO (955_)
  2. ALFA ROMEO MITO (955_)
  3. FIAT 500 (321_)
  4. FIAT 500 (321_)
  5. FIAT 500 C(321_)
  6. .. etc

If you want to fetch the type column just change $columnvariable to 1 and so on.

huangapple
  • 本文由 发表于 2020年1月6日 16:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定