PHP Simple HTML DOM获取两个不同的单元类(内联),但其中一个可能为空

时间:2016-12-23 14:06:54

标签: php dom simple-html-dom

好的,我为此寻找并试了很多东西(3个小时)但是,我仍然难倒。

以下是来自somepage.com的HTML代码段。

<table class="infotable" cellpadding="0" cellspacing="0" width="185">
    <tr>
        <td class="what"">First name:</td>
        <td class="whatdet">Jim</td>
    </tr>
    <tr>
        <td class="what">Last name:</td>
        <td class="whatdet">Bo</td>
    </tr>
    <tr>
        <td class="what">Age:</td>
        <td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
    </tr>
    <tr>
        <td class="what">Sex:</td>
        <td class="whatdet">Rarely</td>
    </tr>
    <tr>
        <td class="what">City:</td>
        <td class="whatdet"></td> <!--PROBLEM IS HERE WITH EMPTY CELL-->
    </tr>
    <tr>
        <td class="what">State:</td>
        <td class="whatdet">California</td>
    </tr>
</table>

这是我的代码片段,其中包含测试,试图通过isset行向我显示更多信息。 (是的,我显然很无能)

require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');
$i=0;
$tabletitles = array(); /* Get the titles 'what' Cell Names */
$tabledetails = array(); /* Get the Details in 'whatdet' Cells */
$tables = $html->find('table[@class="infotable"]'); /* Where both reside in */

foreach($tables as $table) {
    $titles = $table->find('td[@class="what"]');
    $titlesd = $table->find('td[@class="whatdet"]');

    foreach($titles as $title)  {
        /*UPDATE NOTICED A PROBLEM WITH a character like $ so I added */
        /*will do the same in $titlesd if I can figure out how to get it  */

        $title1 = preg_replace('/([?#^&*()$\\/])/', '\\\\$1', $title);

        echo $title1; /*Changed from $title*/

        if (isset($titlesd[$i])) /*this is just for testing*/
            echo $titlesd[$i].' is either 0, empty, or not set at all';

        /* WHAT I WANT is echo '<tr><td>'. $title .'</td><td>'. $titlesd[$i] . </td></tr>;*/
        $i++;
    }
}

我想尝试的内容:

------------|----------
First name  | Jim
----------- |---------
Last name   | Bo
----------- |---------
Age         |
----------- |---------
Sex         | Rarely
----------- |---------
City        |
----------- |---------
State       | California
----------- |---------

但我现在得到的是:

------------|----------
First name  | Jim
----------- |---------
Last name   | Bo
----------- |---------
Age         | Rarely
----------- |---------
Sex         | California
----------- |---------
City        |
----------- |---------
State       | 
----------- |---------

我似乎无法弄清楚如何分配&#34;空白&#34;到$titlesd[$i]或在循环中跳过它。所以,我不断得到不希望的结果。 (至少可以说)

再次,我恳请一位大师在这里给我另一个非常珍贵的教训。 谢谢..

3 个答案:

答案 0 :(得分:0)

使用

$titlesd = ($table->find('td[@class="whatdet"]')) ? $table->find('td[@class="whatdet"]') : "";

答案 1 :(得分:0)

如果我没有弄错的话,这就是你要做的事情:

require_once 'simpledom/simple_html_dom.php';
$html = file_get_html('http://somepage.com/');

foreach($html->find('table[@class="infotable"]') as $table) {
    foreach($table->find('tr') as $line)  {
        $titles = $line->find('td[@class="what"]', 0);
        $titlesd = $line->find('td[@class="whatdet"]', 0);

        echo '<tr>'
                .'<td>'.htmlspecialchars($titles).'</td>'
                .'<td>'.htmlspecialchars($titlesd).'</td>'
            .'</tr>';
    }
}

我解释一下:

nb:如果没有查找,带索引的->find()将返回NULL,我会假设此代码在最差echo $titlesd NULL并显示没什么

答案 2 :(得分:0)

Blag的答案很好,但可以更简单:

{{ student.previous_school }}