当阅读RSS提要时,我尝试从文本中分离图像和视频的链接。 这里是一个RSS馈送http://stopgame.ru/rss/rss_news.xml
与文本的单独链接
有一些文本,其具有与YouTube链接描述,例如,它与
<br><br>http://www.youtube.com/...
或一些端部具有与图像
<br><a href="link"></a><br>
<br><a href="link"></a><br>
结束,部分视频和图像
<br><br>http://www.youtube.com/...<br>
<br><a href="link"></a><br>
<br><a href="link"></a><br>
我需要分离所有图像链接到AR ray $images
和视频链接到阵列$video
。 现在PHP是这样的代码:
if (preg_match_all("/\<br\>\<a href=\"http:\/\/images.stopgame.ru\/(.*)\"\>\<\/a\>\<br\>/", $item->description, $images)) {
$item->description = preg_replace("/\<br\>\<a href=\"http:\/\/images.stopgame.ru\/(.*)\"\>\<\/a\>\<br\>/", "", $item->description);
} else {
$images = null;
}
if (preg_match_all("/http:\/\/www.youtube.com\/(.*)\<\/p\>/", $item->description, $video)) {
$item->description = preg_replace("/\<br\>\<br\>http:\/\/www.youtube.com\/(.*)\<\/p\>/", "", $item->description);
} else {
$video = null;
}
但它表现出不太好的结果:
的var_dump($视频)返回类似这样:
array(2) { [0]=> array(1) { [0]=> string(46) "http://www.youtube.com/watch?v=ZJc2W8SBE5U
" } [1]=> array(1) { [0]=> string(19) "watch?v=ZJc2W8SBE5U" } }
的var_dump($图像)返回像这样:
array(2) { [0]=> array(1) { [0]=> string(237) "
" } [1]=> array(1) { [0]=> string(188) "news/2013/03/15/1363362690.jpg">
你是想用正则表达式解析HTML? ;-) – MattDiamant 2013-03-17 19:04:18
有没有理由不能使用[DomDocument](http://php.net/manual/en/class.domdocument.php)? – juco 2013-03-17 19:04:46