2010-08-14 57 views
0

想我回声出我怎样才能格之间的具体数据与file_get_content

$url = "http://www.mydomain.com"; 
echo file_get_content($url); 

http://www.mydomain.com有一个div即

<title>sitename</title> 
</head><body> 
Lorem Ipsum....... 
<div id="divname">and here is div content</div> 
Copyright bla bla bla 

不,我只希望取得与ID格之间的内容=” divname“我该怎么办

+0

你打算如何处理:'

and here is div content
and another in a deeper level
' – stillstanding 2010-08-14 20:10:51

+1

1.4.3重复身份证是与发霉的拉面面条50鞭打理由。 – 2010-08-15 05:03:08

+0

@stillstanding:重复的id是无效的xhtml - 嘿必须是唯一的整个文件。如果这种情况出现“你做错了”,马克·B的建议惩罚是合理的。 – prodigitalson 2010-08-15 20:59:45

回答

2
$url = "http://www.mydomain.com"; 
$html = new SimpleXmlElement($url, null, true); 

$content = $html->xpath("//div[@id='divname']"); 

当然,你仍然可以使用file_get_contentscurl如果你想引入错误检查文件的提取。

0

随着Simple HTML DOM Parser

$url = "http://www.mydomain.com"; 
$html = file_get_html($url); 
$ret = $html->find('div[id=divname]'); 
+0

原始PHP没有file_get_html()函数 – stillstanding 2010-08-14 20:14:51

+0

您应该添加您正在使用[Simple HTML DOM Parser](http://simplehtmldom.sourceforge.net/)。 – Gumbo 2010-08-14 20:15:13

+0

是的,有必要使用SImple HTML DOM解析器。 – barroco 2010-08-14 20:19:07

0

$ HTML = file_get_html( 'http://www.mydomain.com');
foreach($ html-> find('div#divname')as $ e)
echo $ e-> innertext;

这里为“divname”是这样,我们已经使用#所以,如果您有任何类,那么你可以使用一个ID(点)