php HTML表到php数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8449116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:44:32  来源:igfitidea点击:

HTML table to php array

php

提问by JDV590

--------------EDIT------------------------

- - - - - - - 编辑 - - - - - - - - - - - -

So i am going with the DOM approach. Here is what I have so far:

所以我将采用 DOM 方法。这是我到目前为止所拥有的:

  <?php function getdata(){
    $contents = file_get_contents('internatdata.htm');
    //create a DOM based off of the string from the html table
     $DOM = new DOMDocument;
   $DOM->loadHTML($contents);

   //get all tr and td
   $items = $DOM->getElementsByTagName('tr');
   $tds = $DOM->getElementsByTagName('td');

   function tdrows($elements){
       $str = "";
       for ($ii =0; $ii < $elements->length; $ii++){
            $str .= $elements->item($ii)->nodeValue . ",";


           }
          return $str;
       }

   for ($i = 0; $i < $items->length; $i++){


       echo tdrows($tds) . "; <br />";

       }

    } 
?>

The issue I am having is that I only want to select the td's from each table row. I am trying to achieve this with a nested loop. unfortunately It is printing the text of every tag on the page how ever many times as there are tags. how can i get it so its only printing the td of each tr and not every td on the dom?

我遇到的问题是我只想从每个表格行中选择 td。我试图用嵌套循环来实现这一点。不幸的是,它在页面上打印每个标签的文本多少次,因为有标签。我怎样才能得到它,所以它只打印每个 tr 的 td 而不是 dom 上的每个 td?



I need to use an html table as the source of my data because I don't have access to the database. I figure to be able to query data from the html table I need create a function to convert the table into an array, or a multidimensional array.

我需要使用 html 表作为我的数据源,因为我无权访问数据库。我想能够从 html 表中查询数据我需要创建一个函数来将表转换为数组或多维数组。

I have the basic Idea I think but I need some help finishing the code to return an array based off the html table.

我有我认为的基本想法,但我需要一些帮助来完成代码以返回基于 html 表的数组。

Also If you have a better way of doing this other than converting the table to an array then please let me know

另外,如果您有更好的方法来做到这一点,而不是将表转换为数组,那么请告诉我

Here is the idea I had so far:

这是我到目前为止的想法:

 <?php
 function getdata(){

    $contents = file_get_contents('data.htm');
    //add delimiters (semicolon for a row and comma for a cell) ???

    $stripped = strip_tags($contents);

    //explode into an array based off the delimiters above ???


    } 
    ?>

回答by jli

I've updated your edit to fix it.

我已更新您的编辑以修复它。

function tdrows($elements)
{
    $str = "";
    foreach ($elements as $element) {
        $str .= $element->nodeValue . ", ";
    }

    return $str;
}

function getdata()
{
    $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>";
    $DOM = new DOMDocument;
    $DOM->loadHTML($contents);

    $items = $DOM->getElementsByTagName('tr');

    foreach ($items as $node) {
        echo tdrows($node->childNodes) . "<br />";
    }
}

getdata();

回答by jakx

One way to make this easier is to use a dom parser http://simplehtmldom.sourceforge.net/.

使这更容易的一种方法是使用 dom 解析器http://simplehtmldom.sourceforge.net/

You are still going to have to extract the information into an array but this will make it easier to iterate through the elements one by one.

您仍然需要将信息提取到数组中,但这将使逐个迭代元素变得更加容易。

回答by I'll-Be-Back

You should consider using XML.

您应该考虑使用 XML。

It is much easier than HTML table and much more sufficient.

它比 HTML 表格容易得多,也更充分。

Example: http://www.php.net/manual/en/simplexml.examples-basic.php

示例:http: //www.php.net/manual/en/simplexml.examples-basic.php