解析并通过foreach循环显示每个节点
问题描述:
我想在php中使用simplexml解析twitter上rss feed的用户名。总共有16个名字,所以我想使用foreach循环来运行解析每一个并返回它们。我唯一的问题是foreach循环。解析并通过foreach循环显示每个节点
$url = file_get_contents("http://search.twitter.com/search.atom?q=basketball");
$source = simplexml_load_string($url);
foreach ($source as $match){
$output = $match->name(0)->nodeValue = substr($match->name(0)->nodeValue, 0, strpos($match->name(0)->nodeValue, ' '));
echo $output;
}
答
不知道你有什么错误。但是,从饲料本身来看,似乎$match->name(0)->nodeValue
是不正确的;因为nodeValue
不是SimpleXML
库的方法,而是Document Object Model
库。
您可以$match->author->name
为->name
将输出替换$match->name(0)->nodeValue
出现的所有节点值的标签<name>
,这是<author>
一个孩子。
请输入'$ source' – diEcho 2011-03-26 08:05:36