2012-11-03 35 views
0

我tring解析HTML使用简单的HTML和删除页面菜单和页脚(例如,我选择http://codex.buddypress.org/developer-docs/the-bp-global/,然后可能是其他网址。)。但我的代码返回Fatal error: Call to a member function find() on a non-object,哪里出错?感谢名单。PHP preg_replace html菜单,页脚

require('simple_html_dom.php'); 
$webch = curl_init(); 
curl_setopt($webch, CURLOPT_URL, "http://codex.buddypress.org/developer-docs/the-bp-global/"); 
curl_setopt($webch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($webch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5'); 
$htmls = curl_exec($webch); 
curl_close($webch); 
$html = str_get_html($htmls); 
$html = preg_replace('#<div(.*?)id="(.*?)head(.*?)"(.*?)>.*</div>#is', '', $html); 
$html = preg_replace('#<div(.*?)class="(.*?)head(.*?)"(.*?)>.*</div>#is', '', $html); 
$html = preg_replace('#<div(.*?)id="(.*?)menu(.*?)"(.*?)>.*</div>#is', '', $html); 
$html = preg_replace('#<div(.*?)class="(.*?)menu(.*?)"(.*?)>.*</div>#is', '', $html); 
$html = preg_replace('#<div(.*?)id="(.*?)foot(.*?)"(.*?)>.*</div>#is', '', $html); 
$html = preg_replace('#<div(.*?)class="(.*?)foot(.*?)"(.*?)>.*</div>#is', '', $html); 
foreach($html->find('a') as $element){ 
    echo $element.'<hr />'; 
} 

回答

0

str_get_html好像它是从HTML DOM Parser的功能。它返回的只是一个字符串,这就是你所要的。 preg_replace需要一个字符串作为输入并返回一个字符串,然后将其设置为$html

你的问题是,你再打电话$html->find,这意味着你期待$html为对象,作为一个由str_get_html回来了,但不是因为你只是把它分配给一个字符串,通过preg_replace返回。

你可能想要的是这两件事情任何一个:

  • DO(使用preg_replace),字符串处理做$html = str_get_html($htmls);之前。在这个声明之后,它不再是一个字符串,你做的任何处理都是无用的和错误的。
  • 使用您正在使用的库中可用的实际工具(简单的HTML DOM解析器,就Google而言)来做任何你正在做的事情。例如,像$html->find('div.menu')->class = '';

我会推荐第二点(如果它是你想要的),因为HTML processing using regular expressions is not a really good idea