如何在使用file_get_contents时从特定标记获取内容

问题描述:

例如,我有一个名为cache.txt的.txt文件。它包含一些信息,我需要从<span class="date">**THESE**</span>标签中获取内容。有任何想法吗? 谢谢。如何在使用file_get_contents时从特定标记获取内容

+2

使用[DOM收杆ER(http://www.google.com/search?q=php+dom+parser)。 – webbiedave 2012-02-14 19:29:18

您可以使用Simple HTML DOM

例子:

cache.txt

<span class="abc">Lorem Ipsum</span> 
<span class="date">THESE</span> 
<span class="def">Dolor sit amet</span> 

file.php

<?php  
include 'simple_html_dom.php';  
$html = file_get_html('cache.txt');  
echo $html->find('span[class=date]',0); //Output: THESE 
?> 

**Check this out it will bring the result** 
//cache.txt 
<span class="date">**THESE**</span> 

//index.php 
<?php 
$dom = new DOMDocument(); 
$dom->load('cache.txt'); 
$xml = simplexml_import_dom($dom); 
$data = $xml->xpath('/span'); 
echo $data[0][0]; 
?>