我们有以下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
正在取代<
AND
<
正在取代"
AND
"
正在取代>
请帮助我尝试找几天的答案,但没有找到答案。
答案 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
答案 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);