HTML table to php array

前端 未结 3 1533
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 06:06

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

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

  

        
相关标签:
3条回答
  • 2020-12-30 06:57

    One way to make this easier is to use a dom parser 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.

    0 讨论(0)
  • 2020-12-30 06:59

    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();
    
    0 讨论(0)
  • 2020-12-30 07:08

    You should consider using XML.

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

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

    0 讨论(0)
提交回复
热议问题