Solved

how to strip out part of a URL?

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

734 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