ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Parse URLs add-on - dynamic segments possible?

March 17, 2011 6:04am

Subscribe [5]
  • #1 / Mar 17, 2011 6:04am

    macpro

    85 posts

    Hi EllisLabs and volx,

    I’m looking into separating a given URL to split into his parts. Parse URLs does seem good so far, but the given URL has two dynamic parts:

    http://www.domain.com/part1/part2/dynPart3/dynPart4

    I need to separate “dynPart3” from the rest. How do I achive this?


    Thank you
    Stefan

    Moved to Development and Programming by Moderator

  • #2 / Mar 17, 2011 6:07am

    Micke Alm

    5 posts

  • #3 / Mar 17, 2011 6:13am

    macpro

    85 posts

    Hi Micke,

    right, I don’t mean a URL rather an URI from another site (via an RSS feed). The {segments} variable are for the EE URL links to use them, i.E. as conditionals.

    Stefan

  • #4 / Mar 17, 2011 3:21pm

    Sue Crocker

    26054 posts

    Hi, Stefan. I’m going to move this over to the development forum where it’s a better fit.

  • #5 / Mar 17, 2011 3:42pm

    Jamie Rumbelow

    546 posts

    You’ll have to write a custom plugin here, or enable PHP in templates and do it inline. Drop this into url_segmenter/pi.url_segmenter.php in your system/expressionengine/third_party directory.

    <?php
    /**
     * SP URL Segmenter
     * Segment URLs
     *
     * by Jamie Rumbelow
     * <a href="http://getsparkplugs.com">http://getsparkplugs.com</a>
     *
     * for <a href="http://ellislab.com/forums/viewreply/870034/">http://ellislab.com/forums/viewreply/870034/</a>
     */
    
    class Url_segmenter {
        public $return_data;
    
        public function Url_segmenter() {
            $this->ee =& get_instance();
            
            $url     = $this->ee->TMPL->fetch_param('url');
            $segment = $this->ee->TMPL->fetch_param('segment');
    
            $url  = parse_url($url, PHP_URL_PATH);
            $segs = explode('/', $url);
    
            $this->return_data = $segs[$segment - 1];
        }
    }

    You can then use this plugin in your template like this:

    {exp:url_segmenter url="http://google.com/sparkplugs/rock" segment="1"}
    // outputs 'sparkplugs'
  • #6 / Mar 17, 2011 3:55pm

    Todd D.

    460 posts

    Nice work Jamie! This could come in handy.

    Surprised there wasn’t an add-on that already achieved this.

  • #7 / Mar 17, 2011 5:02pm

    macpro

    85 posts

    Hi Jamie,

    Thank you very much. I wrapped your code as a plugin for regular use! Excellent work.
    There is one thing. If I use segment=“1” it won’t parse anything. I have to use segment=“2” for “sparkplugs

    I attached the plugin, wich works flawlessly under EE 2.1.3. I renamed it to pi.sp.url_segmenter.php to keep care about your code :D


    Cheers
    Stefan

    *******************
    Edit
    *******************

    Fixed this issue
    Uploaded new version

  • #8 / Mar 17, 2011 5:43pm

    Jamie Rumbelow

    546 posts

    Thanks for zipping it up, I’ll GitHub it at some point. Sorry about that little issue, to fix, replace this line:

    $this->return_data = $segs[$segment - 1];

    With this line:

    $this->return_data = $segs[$segment];
  • #9 / Mar 17, 2011 5:48pm

    macpro

    85 posts

    Hi Jamie,

    thank you. i uploaded the new version with your fix.

    Cheers

  • #10 / Mar 17, 2011 7:47pm

    macpro

    85 posts

    Hi Jamie,

    How do I get your plugin to work inside an entry tag? It gives me an PHP error:

    Message: Undefined index: 1
    Line Number: 35

    Cheers

  • #11 / Mar 17, 2011 7:57pm

    Jamie Rumbelow

    546 posts

    What template code are you using? P.S. I’ll help you get it working but I’m not going to fully support this plugin, it’s something I knocked up quickly 😊

  • #12 / Mar 17, 2011 7:59pm

    Lisa Wess

    20502 posts

    Hey guys,

    I’ve moved this up to Community News as it now includes a new plugin.  Awesome work on that!

    I have removed the plugin, though, inline with our Guidelines.  You’ll need to find a place to host and support your add-on.  I would recommend the ever awesome Devot-ee site.

    Thank you!

  • #13 / Mar 17, 2011 8:25pm

    macpro

    85 posts

    Hi Jamie,

    this is my code. I’m using this in conjunction with Andrew Weaver’s Feed Parser plugin:
    I broke this down to some parts only for testing reasons:

    {exp:ajw_feedparser 
    url="http://feeds.feedburner.com/site/news"
    itempath="/rss/channel"
    cache_refresh="10"
    limit="1"
    }
    
        <h1>{item/title}</h1>
    
        Link: <a href="http://{link}">{link}</a>
    
        URL Segment: {exp:sp_url_segmenter url="{link}" segment="1"}
    {/exp:ajw_feedparser}

    The {link} looks like: http://www.domain.com/1454/an-article


    Cheers
    Stefan

  • #14 / Mar 18, 2011 4:44pm

    Jamie Rumbelow

    546 posts

    Thanks Lisa!

    Stefan, Try adding parse=“inward” to the feed parser tag, that should change the parse order and make sure that the {link} variable is parsed correctly. In the mean time I’ll get SP URL Segmenter up on GitHub and add a bit of error checking so no errors occur.

    Jamie

  • #15 / Mar 18, 2011 5:21pm

    macpro

    85 posts

    Hey Jamie,

    you are just awesome! Your hint did it. For all: Keep an eye on the Quotes. You need to re-format them.
    Excellent choice to host this plugin at GitHub. Need an account yet 😉


    Thank you very much. I really appreciate!
    Stefan

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases