我有一个RSS源,我正试图通过SimplePie(在WordPress中)提取数据。
我必须提取内容标记。它适用于<?php echo $item->get_content(); ?>
。它抛弃了所有这些东西(当然这只是一个条目,其他的具有相同的结构):
<table><tr valign="top">
<td width="67">
<a href="http://www.anobii.com/books/Lapproccio_sistemico_al_governo_dellimpresa/9788813230944/014c5c45a7ddaab1ec/" style="border: 1px solid #333333">
<img src="http://image.anobii.com/anobi/image_book.php?type=3&item_id=014c5c45a7ddaab1ec&time=0">
</a>
</td><td style="margin-left: 10px;padding-left: 10px">[person name] put "[title]" onto shelf<br/></td></tr></table>
虽然我需要的只是src =“”标签内的内容(图片网址)。我怎样才能只提取它?
答案 0 :(得分:2)
您可以使用DOMDocument(最佳方式):
$doc = new DOMDocument();
@$doc->loadHTML($html);
$imgs = $doc->getElementsbyTagName('img');
$res = $imgs->item(0)->getAttribute('src');
print_r($res);
使用正则表达式(糟糕的方式):
if (preg_match('~\bsrc\s*=\s*["\']\K[^"\']*+~i', $html, $match))
print_r($match);