2010-08-14 32 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(點)