我对此代码有以下错误,我希望它从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
答案 0 :(得分:0)
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');
?>