如何解析RSS提要中的命名空间条目?
使用这个RSS种子http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml如何解析RSS提要中的命名空间条目?
我如何得到<media:credit>Pete Kiehart for The New York Times</media:credit>
只输出“Pete Kiehart”?
所以我得到了这个。这是一个获取图像和文本的RSS XML设备。所以这样的:
成为该
代码做的是这样的:
<?php
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
$feeds = file_get_contents($url);
$rss = simplexml_load_string($feeds);
$items = [];
foreach($rss->channel->item as $entry) {
$image = '';
$image = 'N/A';
foreach ($entry->children('media', true) as $k => $v) {
$attributes = $v->attributes();
if (count($attributes) == 0) {
continue;
} else {
$image = $attributes->url;
}
$content_data = (string)$entry->children("media", true)->description;
$credit = (string)$entry->children("media", true)->credit;
}
$items[] = [
'link' => $entry->link,
'title' => $entry->title,
'image' => $image,
'Desc' =>$content_data,
'credit' =>$credit,
];
}
$i=0;
foreach ($items as $item) {
if ($i < 3) {
printf('<img src="%s">', $item['image']);
printf('<a href="%s">%s</a>', $item['link'], $item['title']);
printf('<p>%s</p>', $item['Desc']);
printf('<p>credit to: %s</p>', $item['credit']);
$i++;
}
}
?>
我想接下来做的就是转变信用信息逼到名字。所以:
printf('<p>credit to: %s</p>', $item['credit']);
其输出[功劳:皮特Kiehart为纽约时报]这 变为[信用:皮特Kiehart。
我该怎么做这个或那个?
编辑:
的一种方式做到这一点作为也由friend at Discord从字符串获得两个词提及。
的代码是
<?php
$message="AB 1234 Hello, how are you?";
print_r(preg_split('/[\s,]+/', $message, 3));
我试图用那一个,但我不能似乎将它放入自己的代码。使其在信用信息上工作。
编辑2:好的我到了某个地方。
使用:
$credit = preg_split('/[\s,]+/', $credit);
在$信贷=(字符串foreach循环..
和
print_r(array_values($item['credit']));
,而不是
printf('<p>credit to: %s</p>', $item['credit']);
这是迄今为止的结果:
编辑3:获取更近。
使用代码:
print_r(array_values($item['credit']));
?> <br><b>New output!: </b> <?php
echo implode(" ",$item['credit']); ?>
<br><br> <?php
$i++;
}
} ?>
事情是这样的:
<?php
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
$feeds = file_get_contents($url);
$rss = simplexml_load_string($feeds);
$items = [];
foreach($rss->channel->item as $entry) {
$image = '';
$image = 'N/A';
$description = 'N/A';
$credit = 'N/A';
foreach ($entry->children('media', true) as $k => $v) {
$attributes = $v->attributes();
if ($k == 'content') {
if (property_exists($attributes, 'url')) {
$image = $attributes->url;
}
}
if ($k == 'description') {
$description = $v;
}
if ($k == 'credit') {
$parts = explode('for', $v);
if (count($parts) > 1) {
$credit = $parts[0];
} else {
$credit = $v;
}
}
}
$items[] = [
'link' => $entry->link,
'title' => $entry->title,
'image' => $image,
'Desc' => $description,
'credit' => $credit,
];
}
$i=0;
foreach ($items as $item) {
if ($i < 3) {
printf('<img src="%s">', $item['image']);
printf('<a href="%s">%s</a>', $item['link'], $item['title']);
printf('<p>%s</p>', $item['Desc']);
printf('<p>credit to: %s</p>', $item['credit']);
$i++;
}
}
?>
东西是很好的。但是你能否提供一种格式化代码的方法,以便只显示名称而不是'array'[0] [1]不显示。 – xxxx
将代码添加到您的循环中 – alistaircol
已添加。得到同样的结果。 “信用:皮特Kiehart为纽约时报” – xxxx
我看着这一点 - > Stackflow关于 '获得前两个字从PHP字符串' @ HTTP: //*.com/questions/13263523/getting-first-two-words-from-string-in-php - 但我似乎不能够“乐高”的功能正常到我的创作。 – xxxx
[从PHP字符串获得头两个字]的可能的复制(http://*.com/questions/13263523/getting-first-two-words-from-string-in-php) – Trasiva