dqwh1119 2013-02-14 18:38
浏览 104
已采纳

php在使用SimpleHTMLDOM渲染之前抓取页面的内容

OK, the title is vague but i just dont know how to word this.

what I want to do is parse the entire page before rendering, then select a certain div from the page using Simple HTML DOM PHP and render that instead of the entire page, if the header of the page matches through if(isset($_SERVER[] call

Trouble is, i dont know how to do it, i cant select the current page using PHP (using $_SERVER[]?). I dont even know if I'm making sense.

say we start with how the external library works:

$html = file_get_html(''); // Get HTML of this page.. somehow
$div = $html->find('div[id=mainContent]'); // or 'div#mainContent'

But how do I echo the contents of that div? and will this render the page first?

Can anyone help me?

  • 写回答

3条回答 默认 最新

  • dongxingdu9594 2013-02-14 20:29
    关注

    You can obtain this in a cleaner way using, as suggested, ob_get_contents. With appropriate use of core PHP directives and inclusion, you can make it so you don't have to touch the existing PHP files at all!

    <?php
        // This goes before the page, and this can be done automatically (i.e. without
        // modifying the existing PHP file) using auto prepend:
        // http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
        ob_start();
    ?>
    
    // The "old" page ...
    
    <?php
        // This goes after the page, and again can be done automatically using
        // auto append (see above).
        // This 'if' can also check whether this page is one of those that must be
        // manipulated, or not.
        if(!isset($_SERVER['EXAMPLE_HEADER']) || ('false'==$_SERVER['EXAMPLE_HEADER']))
        {
            $html = ob_get_clean();
    
            // Include is only necessary in this scope -- full path may be needed, though
            include_once('simple_html_dom.php');
    
            // Here, $html is parsed to yield a DOM object
            // ...
    
            foreach($dom->find('div[id=mainContent]') as $div)
            {
                echo $div->innertext;
            }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算