简体   繁体   English

如何在 php 中的简单 html dom 中创建 dom 表?

[英]how to dom table in simple html dom in php?

hi i have this html code and i want to get the 76.000.000.000 data on that.嗨,我有这个 html 代码,我想获得 76.000.000.000 的数据。 i search the div#TopBox but it shows nothing.(means no children found on it) html code:我搜索了 div#TopBox 但它什么也没显示。(意味着没有找到孩子) html 代码:

<div id="TopBox">
<div id="divSupervision" style="color:#800040;font-size:12px;vertical-align:middle;">
</div>
<div class="box2 zi1">
   <div class="box6 h80">
      <table>
         <tbody>
            <tr>
               <td>Total</td>
               <td>
                  <div class="ltr inline" title="76,000,000,000">76 B</div>
               </td>
            </tr>
         </tbody>
      </table>
   </div>
   </div>
</div>

my code:我的代码:

$html = str_get_html($curl);
$div = $html->find("div#TopBox");

I don't know what's a problem with 'str_get_html' function, it seems the method from WP, but you can get this data using RegExp:我不知道 'str_get_html' function 有什么问题,这似乎是 WP 的方法,但您可以使用 RegExp 获取此数据:

preg_match('/title="([\d,]+)"/', $html, $matches);
var_dump($matches[1]);

You can find the div inside div#TopBox and get its attribute title :您可以在div#TopBox中找到 div 并获取其属性title

$html = str_get_html($curl);
$total = $html->getElementById("TopBox")->find('div.inline')[0]->getAttribute('title');
echo $total; // 76,000,000,000

Note that you might test the length of array before accessing elements ( [0] ).请注意,您可能会在访问元素( [0] )之前测试数组的长度。

this code work on local html此代码适用于本地 html

$html = file_get_html("file.txt");
$total = $html->getElementById("TopBox")->find('div.inline')[0]->getAttribute('title');
echo $total; // 76,000,000,000

but when i use it to fetch data online it doesn't work why?但是当我使用它在线获取数据时它不起作用,为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM