解析HTML

时间:2016-06-02 21:36:56

标签: php arrays

我正在使用cURL,一旦执行,我最终得到一个变量$data,其中包含整个页面的html内容。

我在该变量中有一些html内容内容可供演示。

<table class="TabTopGroup" width="100%" height="100%" cellspacing="0" cellpadding="0" border="0">
    <tr>
        <td align="Left" class="HtmlGridCell" colspan="5"> </td>
        <td align="Left" class="HtmlGridCell" colspan="2"><span class="progress" title="0%"><span class="indicator" style="width: 0%"> </span></span></td>
    </tr>
    <tr valign="top">
        <td align="Left" class="HtmlGridCell no-bottom-border"><span class="priority-1"> </span></td>
        <td align="Left" class="HtmlGridCell no-bottom-border"><a href="jobview.aspx?id=12514845" class="link">J000005</a></td>
        <td align="Left" class="HtmlGridCell no-bottom-border">Student</td>
        <td align="Left" class="HtmlGridCell no-bottom-border">test job</td>
        <td align="Left" class="HtmlGridCell no-bottom-border"><span id='jobstate_12514845'>Planned</span><span class='inline-dropdown' onclick='return jqe.c();' onmouseover='jqe.s(12514845, this, event);'> </span></td>
        <td align="Left" class="HtmlGridCell no-bottom-border">02-Jun</td>
        <td align="Left" class="HtmlGridCell no-bottom-border">02-Jun</td>
    </tr>
    <tr>
        <td align="Left" class="HtmlGridCell" colspan="5"> </td>
        <td align="Left" class="HtmlGridCell" colspan="2"><span class="progress" title="0%"><span class="indicator" style="width: 0%"> </span></span></td>
    </tr>
    <tr valign="top">
        <td align="Left" class="HtmlGridCell no-bottom-border"><span class="priority-1"> </span></td>
        <td align="Left" class="HtmlGridCell no-bottom-border"><a href="jobview.aspx?id=12514850" class="link">J000006</a></td>
        <td align="Left" class="HtmlGridCell no-bottom-border">Student</td>
        <td align="Left" class="HtmlGridCell no-bottom-border">test job</td>
        <td align="Left" class="HtmlGridCell no-bottom-border"><span id='jobstate_12514850'>Planned</span><span class='inline-dropdown' onclick='return jqe.c();' onmouseover='jqe.s(12514850, this, event);'> </span></td>
        <td align="Left" class="HtmlGridCell no-bottom-border">02-Jun</td>
        <td align="Left" class="HtmlGridCell no-bottom-border">02-Jun</td>
    </tr>
</table>

现在另一方面,我有一个包含以下数据类型的数组

$jobs = 
    array( 
        array( 
            jID => "J000005", 
            Name => "Something"
        ),
        array( 
            jID => "J000006", 
            Name => "Something"
        ),
        array(
            jID => "J16453", 
            Name => "Something"
        )
    );

现在我要做的是在html字符串中搜索jID的出现位置。如果找到jID,我需要从其父锚点获取id参数,然后将它们添加到数组中。因此,如果我用HTML交叉检查上面的数组,我最终会得到类似的东西。

$outcome = 
    array( 
        array( 
            jID => "J000005", 
            aID => "12514845"
        ),
        array( 
            jID => "J000006", 
            aID => "12514850"
        )
    );

我上面显示的示例是一个小数据集。 html字符串有更多的数据,我的初始数组将包含大约50个jID。

我真的在建议处理这个问题的最佳方法。我最初想的是使用DomDocument,但我不认为这是最好的方法。另一种选择是以某种方式使用preg_match_all,但我不太确定它会有多高效。

我面临的另一个问题是html可能包含多个jID。我并不担心J000005会出现多少次,我想要的只是它的关联id,它包含在其父锚中作为参数。

所以关于如何实现这一点的任何建议都表示赞赏。我有兴趣了解最有效的方法是什么,因为我读取preg_match_all比通过DomDocument这样做更快。

0 个答案:

没有答案
相关问题