Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5953317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:48:29+00:00 2026-05-22T17:48:29+00:00

I have URLs like below that need to be recognized by the PHP code.

  • 0

I have URLs like below that need to be recognized by the PHP code. Based on what the URL is, data needs to be shown:

www.example.com/music/username/popular
http://www.example.com/music/username/recent/
http://example.com/music/username/favorites/ignore_this /*Ignore everything after favorites*/
http://www.example.com/music/2011/05/02 /*Shows all music uploaded on this date*/
www.example.com/groups
http://www.example.com/groups/jazz
http://example.com/places/japan/?param=ignore_this /*Ignore everything after japan*/
www.example.com/search/rock/

The first URL should show a user’s popular music. www.example.com/groups should list all public groups. And so on..

  • http:// is optional
  • / at the end is optional
  • If anything (like groups) is entered in UPPERCASE, it should be converted to lowercase

What is the best way to recognize these URLs, using regex and create a Switch case? Example snippet would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:48:30+00:00Added an answer on May 22, 2026 at 5:48 pm

    Here is the system I use (it’s OOP, but you can change easily if you don’t like classes).

    if($this->request->uriMatch('#^/$#'))   //simplest regexp, no substring is matched
        $this->home();  //show the Home page
    elseif($this->request->uriMatch('#^/news/(\d+)\.html$#')) //matches a number!
        $this->newsItem($this->request->uri(0),0); // calls newsItem() function and passes the first (0th) matched substring (in our case it's number) to it as an argument
    elseif($this->request->uriMatch('#^/news_(\d{4})_(\d{1,2})\.html$#')) //matches 2 numbers
        $this->newsList(0,$this->request->uri(0),$this->request->uri(1)); //passes both numbers to function newsList()
    elseif($this->request->uriMatch('#^/products/latest(?:-(\d+))?\.html$#')) //may match one number, or may not match anything
        $this->products('latest',$this->request->uri(0,1)); //if matched, passes the matched number, if not: passes "1" (as default value)
    elseif($this->request->uriMatch('#^/products/(\d+)(?:-(\d+))?\.html$#')) //may match 1 or 2 numbers, this is a mix of previous 2 cases :)
        $this->products($this->request->uri(0),$this->request->uri(1,1));
    else    //if nothing was matched, then 404!
        $this->response->redirect('/404.html');
    

    Note that (?: ) regexp is a non-matching subpattern, so it doesn’t affect anything.

    One example for your the case you provided:

    if($this->request->uriMatch('#^/music/([a-z0-9]+)/favorites/?#i'))
    

    ? means the last / may not exist. Note that there is no $ sign at the end, which means everything after favorites will be ignored. The i modifier (after #) means the text case is not important.

    $this->request is an instance of class Request, here it is:

    class Request{
        private $uri;   //this holds the URI
        private $uriArray;  //this will hold the matched substrings of the URI according to our REGEXPs
        public function __construct(){
            // initializes URI, it doesn't contain http:// and the domain!
            $this->uri = $_SERVER['REQUEST_URI'];
        }
        public function uriMatch($regex){
            // parses URL according to REGEX
            $b = preg_match($regex, $this->uri, $this->uriArray); // $b is false, if the URL was not matched
            if($b==1)   //if $b is not false, uriArray contains the URL AND the matched substrings (http://am.php.net/manual/en/function.preg-match.php).
                array_shift($this->uriArray); // we are removing the first element (which is the URL), we need only matched substrings
    
            return $b==1; //returns true if and only if the URL was matched!
        }
    
        public function uri($n, $default=false){
            //returns n-th matched substring, or $default, if it was not set
            // ... one can add some error handling here
            return isset($this->uriArray[$n]) ? $this->uriArray[$n] : $default;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site with URLs like this: http://domain.co.uk/subdir/page.php I have redesigned the site
I see many, many sites that have URLs for individual pages such as http://www.mysite.com/articles/this-is-article-1
I have a PHP application that will on occasion have to handle URLs where
I have some user-specific data that I need to store in SharePoint and make
The general problem: We have urls coming to our IIS web servers formatted like:
I would like to have pretty URLs for my tagging system along with all
I am developing an application, and have URLs in the format www.example.com/some_url/some_parameter/some_keyword . I
I have a need to use extensionless URLs. I do not have access to
I have URLs of the form http://domain/image/⟨uuid⟩/42x42/some_name.png . The Web server (nginx) is configured
I'm having a bit of a problem here. We have 2 urls let me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.