在外部RSS提要上随机化项目

时间:2018-02-06 22:55:25

标签: php regex rss

亚马逊Feed有10个项目。我有一个代码来修改此Feed,但我希望项目结果是随机的。我希望饲料中的所有10个项目都是随机抽取的。这可能吗?

这是代码

<?php
    $rss = new DOMDocument();
    $rss->load('https://www.amazon.com/gp/rss/bestsellers/3741561/');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = 10;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $title = str_replace('#1: ', '', $title);
        $title = str_replace('#2: ', '', $title);
        $title = str_replace('#3: ', '', $title);
        $title = str_replace('#4: ', '', $title);
        $title = str_replace('#5: ', '', $title);
        $title = str_replace('#6: ', '', $title);
        $title = str_replace('#7: ', '', $title);
        $title = str_replace('#8: ', '', $title);
        $title = str_replace('#9: ', '', $title);
        $title = str_replace('#10: ', '', $title);
        $link = $feed[$x]['link'];
                $link = str_replace('ref=', '&amp;ref=;', $link);
        $description = str_replace(' & ', ' &amp; ', $feed[$x]['description']);
        $description = $feed[$x]['desc'];
        $description = str_replace('<div style=', '<![CDATA[<div style=', $description);
        $description = str_replace('current rank.)', 'current rank.)]]>', $description);
        $description = str_replace('._SL160_.', '.', $description);
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        $amazonUrl = $link;
        preg_match_all('/dp(.*?)&/i', $amazonUrl, $match, PREG_PATTERN_ORDER);
        $newUrl = rawurldecode($match[1][0]);
        echo '<item>';
        echo '<title>'.$title.'</title>';
        echo '<link>http://kinyti.com/product'.$newUrl.'</link>';
        echo '<description>'.$description.'</description>';
        echo '<pubDate>'.$date.'</pubDate>';
        echo '</item>';
        }
?>

1 个答案:

答案 0 :(得分:0)

是的,这是可能的:只需在将shuffle($feed)添加到新阵列之前运行Error: (-215) DataType<_Tp>::type == type() && cols == 1 in function push_back