PHP preg_match_all什么也没有返回
问题描述:
我试图从HTML字符串中提取所有img标签。看到代码PHP preg_match_all什么也没有返回
$d1 = file_get_contents("http://itcapsule.blogspot.com/feeds/posts/default?alt=rss");
preg_match_all('/<img[^>]+>/i',$d1,$result);
print_r($result);
,其结果是
Array ([0] => Array ())
但同样的正则表达式给出正确的结果在网上正则表达式测试工具http://regex.larsolavtorvik.com/。
可能是什么问题?
答
你正在分析的内容进行编码的HTML实体 - 基本上<
被替换为<
。首先使用html_entity_decode将数据转换为正常的html。 PS:使用HTML解析器而不是正则表达式。
+0
谢谢!会尝试 – Orion 2010-02-28 20:14:27
答
使用了SimplePie XML解析器
include_once 'simplepie.inc';
$feed = "feedurl";
$data = new SimplePie($feed);
$data->init();
$data->handle_content_type();
foreach ($data->get_items() as $item)
{
$desc=$item->get_description();
preg_match_all('/<img[^>]+>/i',$desc,$result);
print_r($result);
}
这正是我一直在寻找:)谢谢你们解决这个问题!
谢谢,让我试试 – Orion 2010-02-28 20:11:53