我正在尝试构建一个简单的应用程序来教自己php,我想从另一个站点引用一段html,例如div:
页面上的 <div id="name"> <p>Sam</p> </div>
&#34; http://www.example.com/about.php"
我正在尝试的是<?php echo file_get_contents("http://www.example.com #name"); ?>
是正确的方法吗?
如果最好使用include
echo
或echo file_get_contents
答案 0 :(得分:0)
可以使用DOMDocument
完成,这是一个例子:
$doc = new DOMDocument();
$doc->loadHTMLFile('http://example.com/');
// get the element that has id=name
$result = $doc->getElementById('name');
echo $result->nodeValue; // prints: Sam
// or print out $result to see all of the properties
print_r($result);