1

このコードで次のエラーが発生しました。ファイル全体ではなく、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
4

1 に答える 1

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');

?> 
于 2012-11-30T15:55:24.593 に答える