【问题标题】:Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\index.php on line 7致命错误:在第 7 行的 C:\xampp\htdocs\index.php 中的非对象上调用成员函数 find()
【发布时间】:2012-11-30 15:49:48
【问题描述】:

这段代码有以下错误,我希望它从 HTML 中抓取一个选定的对象,而不是整个文件。

php 文件:

<?php 
include_once('simple_html_dom.php');

$target = "test.html";
$html = file_get_contents($target);

foreach($html->find('div.article') as $element) 
       echo $element->find('h1');

?> 

test.html:

<html>
<head>
</head>
<body>
<div class="article">
<h1>Header</h1>
<p>Paragraph</p>
</div>
</body>
</html>

输出:

Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\index.php on line 7

【问题讨论】:

    标签: find file-get-contents


    【解决方案1】:

    file_get_contents() 返回一个字符串对象。

    字符串对象没有 find 方法。参考http://php.net/manual/en/book.strings.php

    改用这个

    file_get_html()

    <?php 
    include_once('simple_html_dom.php');
    
    $target = "test.html";
    $html = file_get_html($target);
    
    foreach($html->find('div.article') as $element) 
           echo $element->find('h1');
    
    ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2016-03-26
      • 1970-01-01
      • 2014-09-30
      • 2017-04-30
      • 1970-01-01
      相关资源
      最近更新 更多