How to get content from specific tags when using file_get_contents

可紊 提交于 2019-12-23 01:39:28

问题


For example i have one .txt file, called cache.txt. It contains some information, and i need to get content from <span class="date">**THESE**</span> tags. Any ideas? Thanks.


回答1:


You can use Simple HTML DOM

Example:

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
?>



回答2:


**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];
?>


来源:https://stackoverflow.com/questions/9282931/how-to-get-content-from-specific-tags-when-using-file-get-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!