Solved

how to strip out part of a URL?

Posted on 2006-10-20
4
243 Views
Last Modified: 2008-03-17
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
Comment
Question by:equentin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17774921
What about:

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

-r-
0
 

Author Comment

by:equentin
ID: 17775037

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
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 17775313
Hi,

Is index.php in all urls?

Then you could use:

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

-r-
0
 

Author Comment

by:equentin
ID: 17787098
perfect, thank you!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question