过滤simplexml结果

时间:2011-06-27 08:15:28

标签: php simplexml

如何过滤结果以仅显示带有关键字“848”的torrent 我的代码

<?php
$url = "http://www.animesuki.com/xml.php?type=groups&id=1157";
$xml = simplexml_load_file($url) or die("Could not load file!"); 
header('Content-Type: text/xml');
echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
echo "<channel>";
$i = 0;
foreach ($xml->release as $getrelease => $value)
{
    if (++$i > 10) {
        break;
    }
   echo "<item>";
   echo  "<title>".$getrelease->filename."</title>";
   echo "<link>".$getrelease->torrent[0]->direct."</link>";
   echo "</item>";
}
echo "</channel>";
?>

感谢

1 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点。

1)例如,如果文件名包含关键字848;你应该这样做:

<?php if($getrelease->filename == '848') { ?>

2)如果你还想匹配foobar848和bla848blabla,你应该使用正则表达式:

<?php if(preg_match('/848/', $getrelease->filename)) { ?>

但我想这是非常基本的PHP内容,如果你不理解if-loop的概念,也许你应该首先阅读一些书:)。

相关问题