需要帮助从pinterest rss feed中提取图像src url

时间:2012-08-20 18:33:48

标签: rss expressionengine pinterest magpie

基本上我正在尝试将一个Pinterest Feed小部件添加到ExpressionEngine站点。我发现this tutorial非常有用,但问题是我不能使用引用的Tag Stripping插件,因为我正在帮助的网站使用ExpressionEngine 1x而插件仅用于2x。

如果您查看Pinterest rss Feed的来源,例如:http://pinterest.com/amazon/feed.rss,您会看到<description>标签内的Pinterest图片的网址以及图钉的标题,标签和亲属链接到pinterest上的引脚。我希望能够在'img src'之后只提取双引号之间的值。

本教程还使用了一个名为Magpie的插件,该插件已经加载到网站上,所以我很擅长。

理想情况下,我希望我的代码看起来像这样:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" parse="inward"
refresh="720" limit="5"} {items}<a href="{link}" target="_blank"><img
src="<?php code-to-grab-the-img-src->{description} ?>"
alt="{title}"></a>{/items} {/exp:magpie}

显然“代码抓取-img-src-&gt; {description}”不起作用,所以我正在寻找一个函数或其他东西。如何在不使用SuperGeekery Tag Stripper插件的情况下告诉Magpie仅提取img src值?

谢谢!

3 个答案:

答案 0 :(得分:1)

我建议构建一个简单的插件来处理这个问题,你可以使用与my other answer相同的正则表达式(由于在将{description}内容导入PHP时出现问题而失败)。

以下是您的插件的样子:

<?php

$plugin_info = array(
  'pi_name' => 'Get Pinterest Image',
  'pi_version' => '1.0',
  'pi_author' => 'Derek Hogue',
  'pi_author_url' => 'http://amphibian.info',
  'pi_description' => 'Grabs the image URL from a Pinterest RSS feed description element.',
  'pi_usage' => Get_pinterest_image::usage()
  );

class Get_pinterest_image
{

var $return_data = "";

  function Get_pinterest_image()
  {
    global $TMPL;
    $fallback = $TMPL->fetch_param('fallback_image');
    preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", $TMPL->tagdata, $matches);
    $this->return_data = (!empty($matches)) ? $matches[1] : $fallback;
  }

  function usage()
  {
  ob_start(); 
  ?>
    {exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}

    Where {description} is the "description" XML node from the Pinterest RSS feed (likely parsed via the Magpie plugin).
  <?php
  $buffer = ob_get_contents();
  ob_end_clean(); 
  return $buffer;
  }  

}

?>

根据EE惯例,这将命名为 get_pinterest_image.php ,然后进入 / plugins / 文件夹。然后在你的模板中:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5" parse="inward"}
    {items}
        <a href="{link}" target="_blank"><img src="{exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}" alt="{title}" /></a>
    {/items}
{/exp:magpie}

答案 1 :(得分:0)

当然 - 在输出上启用PHP,然后使用此正则表达式:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5"}
{items}
<?php
    preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", '{description}', $matches);
    $src = (!empty($matches)) ? $matches[1] : '/path/to/default-image.png';
?>
    <a href="{link}" target="_blank"><img src="<?php echo $src; ?>" alt="{title}"></a>
{/items}
{/exp:magpie}

此代码包含另一张图片的后备广告,以防因某些原因无法找到图片src

未经测试,但应该可以使用。

答案 2 :(得分:0)

RSS解析很难!如果您愿意更复杂一点并将提要内容带入EE,那么我对仅使用EE2的DataGrab运气好,所以您可以通过免费的EE1前身FeedGrab找到一些快乐。

相关问题