包括使用php echo的另一个站点的特定html元素

时间:2012-10-09 18:20:57

标签: php html include echo include-path

我是全新的PHP(昨天开始)我有一个体面的背景html / css和一些js所以我虽然id给它一个镜头。

我正在尝试构建一个简单的应用程序来教自己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 echoecho file_get_contents

,我也无法解决问题 抱歉这个基本问题

1 个答案:

答案 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);