使用php从html表中检索数据

时间:2014-01-26 20:18:55

标签: php html html-table html-tableextract

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

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

    <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(数字)(在每个数据集之后)。

任何人都可以了解我如何做到这一点吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我通常建议使用像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);

希望有所帮助!

相关问题