您好我有以下php代码,从输出$ doc我想要获取所有[content]项目并将它们放在一起用于标签云。如何将所有[content]项目提取到一个变量中?
$ch = curl_init("http://api.sysomos.com/hb/v1/heartbeat/rss?startid=0&hbrss=DUpnCTqppKjx4q1U6FXd5SWOPFUa2ltLm1haXJAYi1hbmQtcS5jby51aw..&maxC=6&max=30&hid=2535& fTs=me&dRg=7&tab=beats&scO=Recency&");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
print_r($doc);
答案 0 :(得分:0)
如果没有进一步说明,您似乎无法获取各个<content>
节点的字符串值?如果是这样,请考虑以下事项:
$str = '
<xml>
<content>content1</content>
<somethingElse>DOH!</somethingElse>
<content>content2</content>
<content>content3</content>
</xml>
';
$doc = new SimpleXMLElement($str);
$vals = array();
foreach ($doc->content as $content) {
$vals[] = (string) $content;
}
print_r($vals);
这将输出:
Array
(
[0] => content1
[1] => content2
[2] => content3
)
<强>更新强>
根据您的评论,您可以使用上述相同的策略,并执行额外的implode
调用:
$vals = implode('', $vals);
echo $vals; // outputs: content1content2content3
或者,修改原始循环:
$vals = '';
foreach ($doc->content as $content) {
$vals .= (string) $content;
}
echo $vals; // outputs: content1content2content3