Solved

how to strip out part of a URL?

Posted on 2006-10-20
4
236 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now