2015-12-23 41 views
-6

如何分別解析以下代碼中每個頭的內容。如何解析php代碼中的html內容?

$str="<html> 
      <body> 
      <h1>Java</h1> 
      <p>Java is a platform independent object oriented programming language</p> 
      <h2>Html</h2> 
      <p>HTML is a markup language for describing web documents</p> 
      <h3>Php</h3> 
      <p>Php is simple</p> 
      </body> 
      </html>"; 
+0

你看過這個嗎? – Carlo

+1

你想要做什麼? –

+0

使用DOMDocument – RamRaider

回答

2

使用DOMDocument & XPath很簡單。

$str=" 
<html> 
    <body> 
     <h1>Java</h1> 
     <p>Java is a platform independent object oriented programming language</p> 
     <h2>Html</h2> 
     <p>HTML is a markup language for describing web documents</p> 
     <h3>Php</h3> 
     <p>Php is simple</p> 
    </body> 
</html>"; 

$dom=new DOMDocument; 
$dom->loadHTML($str); 
$xp=new DOMXPath($dom); 
$col=$xp->query('//h1|//h2|//h3'); 
foreach($col as $node) echo $node->nodeValue;