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

  • Home
  • SEARCH
  • 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 3402500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:08:44+00:00 2026-05-18T05:08:44+00:00

I am trying to create a url router in PHP, that works like django’s.

  • 0

I am trying to create a url router in PHP, that works like django’s.

The problems is, I don’t know php regular expressions very well.

I would like to be able to match urls like this:

/post/5/
/article/slug-goes-here/

I’ve got an array of regexes:

$urls = array(      
  "(^[/]$)" => "home.index",     
  "/post/(?P<post_id>\d+)/" => "home.post",    
);

The first regex in the array works to match the home page at / but I can’t get the second one to work.

Here’s the code I am using to match them:

foreach($urls as $regex => $mapper) {
    if (preg_match($regex, $uri, $matches)) {
    ...
    }
}

I should also note that in the example above, I am trying to match the post_id in the url: /post/5/ so that I can pass the 5 along to my method.

  • 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-18T05:08:45+00:00Added an answer on May 18, 2026 at 5:08 am

    You must delimit the regex. Delimiting allows you to provide ‘options’ (such as ‘i’ for case insensitive matching) as part of the pattern:

    ,/post/(?P<post_id>\d+)/,
    

    here, I have delimited the regex with commas.

    As you have posted it, your regex was being delimited with /, which means it was treating everything after the second / as ‘options’, and only trying to match the “post” part.

    The example you are trying to match against looks like it isn’t what you’re actually after based on your current regex.

    If you are after a regex which will match something like;

    /post/P1234/
    

    Then, the following:

    preg_match(',/post/(P\d+)/,', '/post/P1234/', $matches);
    print_r($matches);
    

    will result in:

    Array
    (
        [0] => /post/P1234/
        [1] => P1234
    )
    

    Hopefully that clears it up for you 🙂

    Edit

    Based on the comment to your OP, you are only trying to match a number after the /post/ part of the URL, so this slightly simplified version:

    preg_match(',/post/(\d+)/,', '/post/1234/', $matches);
    print_r($matches);
    

    will result in:

    Array
    (
        [0] => /post/1234/
        [1] => 1234
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i am trying to create a URL that looks like this: black/granite/worktops where
I'm trying to create a url pattern that will behave like controller/action/id route in
I am trying to create a validation that checks to make sure a domain/url
I'm trying to learn Regex & URL Rewriting in PHP. How can I create
I'm trying to create a route that looks like this site.com/controller/{Param1}/{dbID}-{friendly-name} and omit the
I'm trying to create a router for a web application (yes, I know solutions
I'm trying to create a route and then programatically retrieve the url of that
I'm trying to create a string from a regular expression. I noticed that in
I am trying to create a route with a Username... So the URL would
So i'm trying to POST something to a webserver. System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); System.String

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.