[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how to strip out part of a URL?

Posted on 2006-10-20
4
Medium Priority
?
245 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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