• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

how to strip out part of a URL?

Hi there ... i have a function that will replace the country code of a URL and works beautifully:

function getLanguageURL($lang) {
    $u = explode("/", $_SERVER['REQUEST_URI']);
    $u[1] = $lang;
    return implode("/", $u);
}

and is called thus: <a href="<?php echo getLanguageURL('fr'); ?>"><strong>France</strong></a>

however we are using expression engine CMS and some URLs contain a category id which is different for each language, ie:

www.site.com/en/index.php/section/C23/
OR
www.site.com/en/index.php/some_section/C23/article-name

how can i amend this function to completely strip out anything beyond "section", if it exisits, and just return:
www.site.com/en/index.php/section/

many thanks!!
0
equentin
Asked:
equentin
  • 2
  • 2
1 Solution
 
RoonaanCommented:
What about:

return preg_replace('#/[^/]+section/#i', '/section/', implode('/', $u));

-r-
0
 
equentinAuthor Commented:

hey R,

thanks but maybe i should have mentioned that "section" is just a generic term. it could be "solutions", "news", "products" or "about", etc

also, the url could be longer, ie:

www.site.com/en/index.php/news/article/C18/merger-press-release/

and in this case i would want to return:

www.site.com/en/index.php/news/

does that make things drastically more complicated?
0
 
RoonaanCommented:
Hi,

Is index.php in all urls?

Then you could use:

preg_replace('#(index.php/[^/]+).*#', '\1', implode('/', $u));

-r-
0
 
equentinAuthor Commented:
perfect, thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now