simple_html_dom找到tbody和其他组件

时间:2014-01-08 15:02:19

标签: php html-table simple-html-dom

我正试图整天处理它,但我无法找到如何解析我的表项目..我有桌子:

<table>
   <tbody>
      <tr>
         <td>
           <img title="this is img which I need also" />
         </td>

         <td>
            <div>
                TEXT WHICH I NEED
            <div>
            <div>
                2nd TEXT WHICH I NEED
            <div>
            <div>
                3rd TEXT WHICH I NEED
            <div>
            <div>
                4th TEXT WHICH I NEED
            <div>
            <div>
                HREF which I need
            <div>
            <div>
                TEXT which I need also
            <div>
         </td>
      </tr>
   </tbody>

</table>

那怎么弄呢?这不是1个表,总共有5个表,这是最后一个(我已经得到它$table = $raw->find('table'); echo $table[4];)但是如何获取所有其他数据?我卡在那里:

print_r($table[4]->find('tbody'));

它返回我的假或没有..

2 个答案:

答案 0 :(得分:1)

我更正了您的HTML示例,除非您说它不是错误!

然后基于此,我们有:

  • 只有一个img代码,因此我们可以直接搜索并获得标题$table->find('img',0)->title

  • 所有其他想要的文本都带有div标记,因此我们可以搜索所有div,然后使用循环打印其内容

这是一个总结上述内容的工作代码:

$table = '
    <table>
       <tbody>
          <tr>
             <td>
               <img title="this is img which I need also" />
             </td>

             <td>
                <div>
                    TEXT WHICH I NEED
                </div>
                <div>
                    2nd TEXT WHICH I NEED
                </div>
                <div>
                    3rd TEXT WHICH I NEED
                </div>
                <div>
                    4th TEXT WHICH I NEED
                </div>
                <div>
                    HREF which I need
                </div>
                <div>
                    TEXT which I need also
                </div>
             </td>
          </tr>
       </tbody>
    </table>';

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($table);

/////////////////
// Find img title
$imgTitle = $html->find('img',0)->title;

echo "IMG title: </br> $imgTitle </br></br>";

////////////////
// Find all divs
$divs = $html->find('div');

echo "DIV's content:</br>";

// loop through all found divs and print their content
foreach($divs as $i => $div) {

    echo "$i: " . $div->plaintext . "<br>";
}

// Clear DOM object
$html->clear();
unset($html);

<强>输出

IMG title: 
this is img which I need also
DIV's content:
0: TEXT WHICH I NEED 
1: 2nd TEXT WHICH I NEED 
2: 3rd TEXT WHICH I NEED 
3: 4th TEXT WHICH I NEED 
4: HREF which I need 
5: TEXT which I need also 

Working DEMO

答案 1 :(得分:0)

这对我有用:

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');

if($emails) {
    foreach($emails as $email_number) {

            $message = base64_decode(imap_fetchbody($inbox, $email_number, 1));
            $html = new simple_html_dom();
            $html->load($message);
            $tds = $html->find('td');

            foreach($tds as $i => $td) {

                echo "$i: " . $td->plaintext . "<br>";

            }

    }
} 

imap_close($inbox);