从rss Feed中的html标记中提取文本

时间:2014-01-30 08:45:08

标签: php xml rss

我们有以下RSS Feed

<title>THIS IS THE TITLE</title>
<link>http://www.website.com/....</link>
<description>
  <div class="primary-image">
   <img typeof="foaf:Image" src="http://website.com/" alt="Drink driving" title="Drink driving" />
  </div>
  <div class="field-group-format group_meta field-group-div group-meta  speed-fast effect-none">
   <span class="field field-name-field-published-date field-type-datetime field-label-hidden">
      <span class="field-item even">
    <span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2014-01-29T17:43:00+00:00">29 Jan, 2014 5:43pm</span>
      </span>
   </span>
   <span class="field field-name-field-author field-type-node-reference field-label-hidden">
      <span class="field-item even"><a href="/authors/joe-finnerty">Joe Finnerty</a></span>
   </span>
  </div>
  <p class="short-desc">TEXT THAT I WANT TO EXTRACT FROM HERE</p>
</description>

我正在尝试使用以下this script提取<p class="short-desc">TEXT THAT I WANT TO EXTRACT FROM HERE</p>并在此处检查了一些问题,但没有找到实际的答案。

我尝试添加

$htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
$html = new DOMDocument();        
$html->loadHTML($htmlStr);
$xpath = new DOMXPath($html);
$desc = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' short-desc')]"); 
<{1>}之前的

,在$item = array (循环内但不起作用。

但没有做好这项工作。而不是 foreach正在取代&lt; AND <正在取代&quot; AND "正在取代&gt;

请帮助我尝试找几天的答案,但没有找到答案。

3 个答案:

答案 0 :(得分:1)

假设您将上述HTML内容传递给$html变量..

 $dom = new DOMDocument;
    @$dom->loadHTML($html);
    foreach ($dom->getElementsByTagName('p') as $tag) {
        if ($tag->getAttribute('class') === 'short-desc') {
            echo $tag->nodeValue; //"prints" TEXT THAT I WANT TO EXTRACT FROM HERE
         }
    }

答案 1 :(得分:0)

如果我理解正确,您想要从Feed中删除标记,以便您可以尝试这样:

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
?>

输出将是:

Test paragraph. Other text

了解更多信息:http://in3.php.net/strip_tags

答案 2 :(得分:0)

为什么不使用正则表达式?

$strRegex = '%<p class="short-desc">(.+?)</p>%s';

if (preg_match_all($strRegex, $strContent, $arrMatches))
{
var_dump($arrMatches[1][0]);
}

并获取内容

$path = 'path/to/file';
$strContent = file_get_contents($path);