【问题标题】:retrieving data from a html table using php使用 php 从 html 表中检索数据
【发布时间】:2014-01-26 20:18:55
【问题描述】:

我知道这个问题已经被问过很多次了,但我研究了很多例子,但我仍然无法从这个 html 表中获取我需要的数据。

我有一个 php 文件,可以生成这样的 html 表:

    <table width="97%">
    <tr><td align="center">
    <!-- table for columns -->
    <table border="0" cellpadding="15">
    <tr>
        <td valign="top">

        <table border="0" width="800">
        <caption style="font-size: 32px; font-weight: bold;">
        </caption>

        <!-- force column widths exactly (for some reason it didn't want to
        play along with normal width settings) -->
        <tr>
        <td><img src="/spacer.gif" width="160" height="1" border="0" alt="" /></td>
        <td><img src="/spacer.gif" width="170" height="1" border="0" alt="" /></td>
        </tr>
            <tr>
                <td style="">
                DATA1
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
                0            </td>
            </tr>

            <tr>
                <td style="">
                DATA2
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
                0            </td>
            </tr>
            <tr>
                <td style="">
                DATA3
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
        0            </td>
            </tr>
            <tr>
                <td style="">
                DATA4
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
                5            </td>
            </tr>
            <tr>
                <td style="">
                DATA5
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
                0            </td>
            </tr>
            <tr>
                <td style="">
                DATA6
                </td>

                <td width="200" style="font-size: 80px; font-weight:bold;">
                0            </td>
            </tr>


        <!-- end of stats_with_style loop -->

        </table>

        </td>



    <!-- end of groups loop -->

    </tr>
    </table>

    <br /><br />


    </td></tr>
    </table>

我想使用 php 获取每个 DATA 集的 html(编号)(在每个 DATA 集的样式之后)。

谁能告诉我如何做到这一点?

【问题讨论】:

    标签: php html html-table html-tableextract


    【解决方案1】:

    我通常建议使用像 Ganon 这样的 DOM 解析器,但是如果这个 HTML 的结构保持相当简单(像这样),那么只使用 PHP 的原生 DOM 和 XPath 选择器可能只是一个更简单、开销更低的解决方案。将您的 HTML 加载到这样的字符串中:

    <?php
    $html = <<<EOF
    <table width="97%">
        <tr><td align="center">
        <!--SNIP-->
    EOF;
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $data = [];
    
    // targets any <td> with a <style> element and only selects odd elements
    // (XPath counting starts at 1)
    foreach($xpath->query("//td[@style][position() mod 2 = 0]") as $node) {
        //replace superflous whitespace in the string
        $data[] = preg_replace('/\s+/', '', $node->nodeValue);
    }
    

    现在您将拥有一个 $data[] 数组,该数组仅包含数值(您要求的)。

    如果您还需要键(DATA1 等...),通过循环遍历偶数元素使其成为关联数组是一项相当简单的工作,只需添加以下代码:

    foreach($xpath->query("//td[@style][position() mod 2 = 1]") as $node) {
        $keys[] = preg_replace('/\s+/', '', $node->nodeValue);
    }
    
    $dataWithKeys = array_combine($keys, $data);
    

    希望有帮助!

    【讨论】:

    • 谢谢!!这太完美了! :)
    【解决方案2】:

    文件正在使用 PHP 生成,但是您想使用 PHP 来获取数据?也许您应该首先将这些数据保存在其他地方,以一种更易于使用 PHP 阅读的格式。

    【讨论】:

    • 我会这样做,但不幸的是原始文件不是我可以直接访问的文件。
    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2015-07-23
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多