douping1993 2016-03-11 23:28
浏览 70
已采纳

简单的HTML DOM解析不起作用

I'm trying to extract emails, names and phone numbers from my html table and use these details in order to send an automatic email reply.

For some reason, I get a fatal error saying: Call to undefined function file_get_html() in http://itecdigital.org.uk/2015/430926/BeautyFactoryBooking/admin.php on line 3

My code for the html Dom parser:

<?php

$html = file_get_html('http://itecdigital.org.uk/2015/430926/BeautyFactoryBooking/admin.php');

$dom = new DOMDocument();
$dom->loadHTML($html);

$elements = $dom->getElementsByTagName('tr');
//Loop through each row
foreach ($rows as $row) {
    //Loop through each child (cell) of the row
    foreach ($row->children() as $cell) {
        echo $cell->plaintext; // Display the contents of each cell - this is the value you want to extract
    }
}

?>

Can anyone see whats wrong with this?

My html code for the table is as follows:

<?php

        echo "<table style='border: solid 1px black;'>";
        echo "<tr><th>Id</th><th>First Name</th><th>Last Name</th><th>Email Address</th><th>Phone Num</th><th>Treatment</th><th>Date</th><th>Time</th><th>Message</th><th>Reply</th></tr>";

        class TableRows extends RecursiveIteratorIterator {
            function __construct($it) {
                parent::__construct($it, self::LEAVES_ONLY);
            }

            function current() {
                return "<td style='width:100px;border:1px solid black;'>" . parent::current(). "</td>";
            }

            function beginChildren() {
                echo "<tr>";
            }

            function endChildren() {
                echo "</tr>" . "
";
            }
        }

        $servername = "#";
        $username = "#";
        $password = "#";
        $dbname = "#";

        try {
            $conn = new PDO("mysql: host=$servername; dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare("SELECT Booking_request_form.id_booking, Client_Information.first_name, Client_Information.last_name, Client_Information.email_address, Client_Information.phone_number, Booking_request_form.treatment, Booking_request_form.date, Booking_request_form.time, Booking_request_form.message FROM Booking_request_form INNER JOIN Client_Information WHERE Client_Information.id_client=Booking_request_form.client_fk"); 

            $stmt->execute();

            // set the resulting array to associative
            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
                echo $v;
            }
        }

        catch(PDOException $e) {
            echo "Error: " . $e->getMessage();
        }

        $conn = null;
        echo "</table>";

?> 

Is there an easy fix to this?

  • 写回答

3条回答 默认 最新

  • douqie1852 2016-03-12 01:05
    关注

    You mix Simple HTML Dom third part class commands (as per your question title) with DOMDocument built-in class commands, so your code can't work.

    file_get_html() is a Simple HTML Dom function, replace it with file_get_contents():

    $html = file_get_contents( '/Users/sam/Downloads/trash.html' );
    
    $dom = new DOMDocument();
    libxml_use_internal_errors( 1 );      // <-- add this line to avoid DOM errors
    $dom->loadHTML( $html );
    
    $elements = $dom->getElementsByTagName('tr');
    

    Now, init an array ($rows) to fill with cells values and a integer string ($cols) for column numbers; your HTML is malformed and this variable will help you to produce a well-formed table:

    $rows = array();
    $cols = 0;
    

    In your code there is another error: you put <tr> in $elements, then you refer it in foreach() using $rows. Then, you call ->children() method to iterate through all children, but DOMElement don't have this method, use ->childNodes property instead. But, first to all, check the row column number and update previously declared variable $cols. Inside nested foreach(), you add cells values to $rows. You will display later. To retrieve values of DOMNode use ->nodeValue instead of ->plaintext. I have wrapped $cell->nodeValue by trim() to remove extra spaces at begin/end of string:

    foreach ($elements as $key => $row)
    {
        if( $row->childNodes->length > $cols ) $cols = $row->childNodes->length;
        foreach( $row->childNodes as $cell )
        {
            $rows[$key][] = trim( $cell->nodeValue );
        }
    }
    

    Now, you have the cells values in multidimensional array $rows.


    Table display

    Your code for displaying table is not your code, it is a copy-and-paste from the net: it has nothing to do with your question and you can ignore it.

    Use a simply code like this instead:

    echo "<table>
    ";
    echo "    <tr>
    ";
    for( $j = 0; $j < $cols; $j++ ) echo "        <th>{$rows[0][$j]}</th>
    ";
    echo "    </tr>
    ";
    for( $i = 1; $i < count($rows); $i++ )
    {
        echo "    <tr>
    ";
        for( $j = 0; $j < $cols; $j++ )
        {
            if( isset( $rows[$i][$j] ) ) echo "        <td>{$rows[$i][$j]}</td>
    ";
            else                         echo "        <td></td>
    ";
        }
        echo "    </tr>
    ";
    }
    echo "</table>
    ";
    

    This is only a working example, modify HTML code as you prefer. You can also change the order of cells. Note the different code between printing table header and printing table rows (for() loop start from 1). Also note the use of $cols: if a cell is empty, we output an empty <td>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来