2012-05-26 41 views
2

可能重複:
How to parse and process HTML with PHP?獲取使用URL元素的特定內容塊

我知道的file_get_contents(URL)方法,但我想的是,也許使用的file_get_contents(URL )首先要拉一個頁面的內容,然後是否有方法/函數可以從您使用file_get_contents(url)獲得的內容中提取或獲取某個內容塊?這裏有一個例子:

這樣的代碼將是這樣的:

$pageContent = file_get_contents('http://www.pullcontentshere.com/'); 

,這將是$pageContent

<html> <body> 
    <div id="myContent"> 
     <ul>  
      <li></li> 
      <li></li> 
      <li></li> 
     </ul> 
    </div> 
</body> </html> 

也許你有什麼建議或心裏有怎樣的輸出專門提取<div id="myContent">和它的整個孩子?

因此,這將是這樣的:

$content = function_here($pageContent); 

所以輸出會是這樣:

 <div id="myContent"> 
      <ul>  
       <li></li> 
       <li></li> 
       <li></li> 
      </ul> 
     </div> 

答案是極大的讚賞!

+1

你可以用'DOMDocument':http://php.net/manual/en/class.domdocument.php –

+0

見http://stackoverflow.com/q/3577641/212218 – 2012-05-26 17:20:36

回答

3

另一種方法是使用正則表達式。

<?php 

$string = '<html> <body> 
    <div id="myContent"> 
     <ul>  
      <li></li> 
      <li></li> 
      <li></li> 
     </ul> 
    </div> 
</body> </html>'; 

if (preg_match ('/<div id="myContent"(.*?)<\/div>/s', $string, $matches)) 
{ 
    foreach ($matches as $key => $match) 
    { 
     echo $key . ' => ' . htmlentities ($match) . '<br /><br />'; 
    } 
} 
else 
{ 
    echo 'No match'; 
} 

?> 

活生生的例子:http://codepad.viper-7.com/WSoWCh

+0

嗨,我喜歡你的答案,簡短。但是,當我嘗試它,它顯示爲文本不是HTML輸出。你知道如何使它作爲html顯示工作? –

+0

@PHPNoob是的,只需刪除htmlentities()函數 – w00

0

您需要使用XML解析來解決您的問題。我會向您推薦SimpleXML,它已經是PHP的一部分。這裏有一個例子:

$sitecontent = " 
<html> 
    <body> 
     <div> 
     <ul>  
      <li></li> 
      <li></li> 
      <li></li> 
     </ul> 
     </div> 
    </body> 
</html>"; 

$xml = new SimpleXMLElement($sitecontent); 
$xpath = $xml->xpath('//div'); 

print_r($xpath); 
2

您可以使用內置的SimpleXMLElement作爲nullpointr的答案解釋,或者你也可以使用正則表達式。 另一個解決方案,我通常發現很簡單的是PHP Simple HTML DOM Parser。你可以在這個庫中使用jQuery風格的選擇器。與您的代碼一個簡單的例子是這樣的:

// Create DOM from url 
$html = file_get_html('http://www.pullcontentshere.com'); 
// Use a selector to reach the content you want 
$myContent = $html->find('div.myContent')->plaintext;