Hello,
I’m running into an issue where Magpie doesn’t appear to be playing well with some PHP in one of my templates.
I’m wondering if perhaps for some reason Magpie can’t be used with preg_replace in a template within the {magpie:items} loop.
I know the regex pattern and preg_replace statement work — if I move it to outside the {magpie:items} loop and hard code in a URL where the {magpie:link} variable is, this works just fine. Does something happen under the hood here that I’m not seeing?
I’ve validated the feed, and found no problems.
Basically, what I’m trying to do is to display a link from an RSS feed, and then use preg_replace to extract the domain name of the site.
Given this link: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history
The preg_replace would return “developer.mozilla.org”.
Here’s the code I’m trying to use:
{exp:magpie url="[feed url here]" limit="10" refresh="720"}
{magpie:items}
<li><a href="http://{magpie:link}">{magpie:title}</a>
<?php
$url_pattern = "/(?:https?:\/\/)(?:www[.])?([a-z0-9.\-]+[.][a-z]{2,4})(\/.*)/";
$url = "{magpie:link}";
$replace = "$1";
$site = preg_replace( $url_pattern, $replace, $url);
?>
<span class="source"><?php echo $site; ?></span>
</li>
{/magpie:items}
{/exp:magpie}So what I should see in the outputted template is this:
<li><a href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history">Manipulating the browser history - MDC Doc Center</a>
<span class="source">developer.mozilla.org</span>
</li>But what I’m seeing this (note the full url on the 3rd line):
<li><a href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history">Manipulating the browser history - MDC Doc Center</a>
<span class="source">https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history</span>
</li>I totally understand that portions of this may be beyond the scope of support here in the forums, but mostly I’m concerned about whether this should be possible within the {magpie:items} loop.
Hi, Douglas -
This is definitely beyond the scope of support, so I will be moving this down to the Dev and Programming forum. Have you considered turning your PHP into a plugin so that you don’t run into issues mixing up PHP and EE tags? You may need the parse= parameter once you do that.
If you look around devot-ee there may be some options for you as well. Check out the search on extract and domain as a start.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.