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 6953787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:31:24+00:00 2026-05-27T14:31:24+00:00

I am trying to build a router function to properly match incoming URI’s and

  • 0

I am trying to build a router function to properly match incoming URI’s and match them to an array of stored system URI’s. I also have wildcards ‘(:any)’ and ‘(:num)’ similar to CodeIgniter.

Basically, I am trying to get the ‘admin/stats/(:num)’ entry to match on both ‘admin/stats’ and admin/stats/1′.

While the script is starting I grab all paths from a separate array and use a foreach to save each path:

route('admin/stats/(:num)', array('#title' => 'Statistics',...));

The function is:

function route($path = NULL, $options = NULL) {

static $routes;

    //If no arguments are supplied, return all routes stored.
    if(!isset($path) && !isset($options)) {
        return $routes;
    }

    //return options for path if $path is set.
    if(isset($path) && !isset($options)) {

        //If we have an exact match, return it.
        if(array_key_exists($path, $routes)) {
            return $routes[$path];
        }
        //Else, we need to use RegEx to find the correct route options.
        else {
        $regex = str_replace('/', '\/', $path);
        $regex = '#^' . $regex . '\/?$#';
            //I am trying to get the array key for $route[$path], but it isn't working.

            // route_replace('admin/stats/(:num)') = 'admin/stats/([0-9]+)'.
        $uri_path = route_replace(key($routes[$path])); //route_replace replaces wildcards for regex.
            if(preg_match($regex, $uri_path)) {
                return $routes[$path];
            }
        }
    }

    $routes[$path] = $options;

    return $routes; 
}

Route replace function:

function route_replace($path) {
    return str_replace(':any', '.+', str_replace(':num', '[0-9]+', $path));
}

A key/value pair in the $routes array looks like:

[admin/stats/(:num)] => Array
    (
        [#title] => Statistics //Page title
        [#access] => user_access //function to check if user is authorized 
        [#content] => html_stats //function that returns HTML for the page
        [#form_submit] => form_stats //Function to handle POST submits.
    )

Thanks for the help. This is my first router and I am not that familiar in making proper Regex’s.

  • 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-27T14:31:25+00:00Added an answer on May 27, 2026 at 2:31 pm

    ‘admin/stats/(:num)’ will never match ‘admin/stats’ as in your “pattern” the slash is required. In pseduo-regex you need to do something like ‘admin/stats(/:num)’.

    There does also seem to be a few bugs in your code. This line

    $uri_path = route_replace(key($routes[$path]));
    

    is in the block that is executed when $path is not a key that exists in $routes.

    I’ve tried to rewrite it and this seems to work (this is just the else clause):

    foreach( array_keys( $routes ) as $route ) {
            $regex = '#^' . $route . '?$#';
            //I am trying to get the array key for $route'$path', but it isn't working.
    
            // route_replace('admin/stats/(:num)') = 'admin/stats/('0-9'+)'.
            $uri_path = route_replace($regex); //route_replace replaces wildcards for regex.
            if(preg_match($uri_path,$path)) {
                return $routes[$route];
            }
        }
    

    But this requires ‘admin/stats/(:num)’ to be ‘admin/stats(/:num)’.

    btw if you don’t have one already, you should get a debugger (Zend and xDebug are two of the most common ones for PHP). They can be invaluable in solving problems like this.

    Also, ask yourself if you need to write a router, or whether you can’t just use one of the perfectly good ones out there already…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build sslsniff on a RHEL 5.2 system here. When compiling sslsniff on
I'm trying to build a short URI service with CI just so I can
I'm trying to build a sort of Wordpress-esque CMS system to a project I'm
We are trying to build simple transacted jms-to-jms router using Mule ESB and JBoss
I'm trying to prepare to build a database driven .net application and I have
I'm trying to build a navigation menu. I am receiving an array with a
I'm trying to build a system working with ADFS and claims. At the moment,
With the Zend Framework, I am trying to build routes for a REST api
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems

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.