您可以按照建议使用更简洁的方式获取此信息ob_get_contents
。通过适当地使用核心 PHP 指令和包含,您可以完全不必触及现有的 PHP文件!
<?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;
}
?>