We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

URL manipulation -- help me find the right plugin

Development and Programming

Brendon Carr's avatar
Brendon Carr
135 posts
16 years ago
Brendon Carr's avatar Brendon Carr

I’m wanting to find the current page’s URL, then take segment 2, the template group, and replace it with some other text – the name of another template group. Can anybody suggest a plugin that does this, or facilitates this?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Hi Brendon,

When you say take segment 2 and replace it exactly what do you need to do with it and how do you mean replace?

You can get hold of the different segments using the {segment_1} / {segment_2} etc variables that are available all the time in ExpressionEngine without the need for a plugin but it all depends on what you mean by replace the text?

Best wishes,

Mark

       
Brendon Carr's avatar
Brendon Carr
135 posts
16 years ago
Brendon Carr's avatar Brendon Carr

Let’s say the current URL is /foo/segment_3/segment_4/ – what I’m looking for is a way to grab that URL and then create a new URL replacing “foo” with “bar”, as in /bar/segment_3/segment_4/ – without knowing in advance how many segments actually exist. The only one I want to replace is the (segment_2}.

This is in relation to a multi-language setup, where the template groups stand for the active language.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Okay,

Let’s say you have this URL :

http://www.example.com/site/en/template-group/template/my_url_title

If you want to fetch the current URL and change the second segment then the easiest way to do this would be :

http://www.example.com/{segment_1}/fr/{segment_3}/{segment_4}/{segment_5}

That should hopefully do it for you although I’m still not entirely sure why you need to do this and where exactly or more importantly at what stage. Do you have a link to the site you are doing this on just so that I can get more of an idea of which stage you need to do this at?

Best wishes,

Mark

       
Matt Weinberg's avatar
Matt Weinberg
489 posts
16 years ago
Matt Weinberg's avatar Matt Weinberg

Make sure you don’t switch segment numbers here Brandon. In your example /foo/ is segment 1 (segment 1 is the template group).

Also, there are some ways using PHP and the various EE classes to grab the URLs and segment values in addition to Mark’s method. Are you comfortable with PHP? Take a look at the “Fetch Segments of the URL” area of this page: http://expressionengine.com/docs/development/usage/input.html.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Yep as slapshotw has mentioned there are a lot more ways of getting at the current URI. I created a plugin some time back that does just that. The link is also in my signature too.

Probably wouldn’t work for what you want to do here but you can take a look at how it works and modify at will if you like.

Best wishes,

Mark

       
Daniel Walton's avatar
Daniel Walton
553 posts
16 years ago
Daniel Walton's avatar Daniel Walton

If it is always the first segment (1) that you want to replace this should suffice.

<?php
global $IN, $FNS;
$new = $FNS->create_url('my-new-first-segment/'.implode('/', array_slice(explode('/', $IN->URI), 1)));
?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.