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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:54:42+00:00 2026-05-19T21:54:42+00:00

I am working on the routing or uri’s in my PHP app. Currently I

  • 0

I am working on the routing or uri’s in my PHP app. Currently I have an array with a regex => url map like this…

<?php
$uri_routes = array( 
    //users/account like http://mysite.com/users/324 (any digit)
    'users/friends/page-(\d+)' => 'modules/users/friends/page-$1',
    'users/friends/' => 'modules/users/friends/',
    'users/online' => 'modules/users/online/' ,
    'users/online/page-(\d+)' => 'modules/users/online/page-$1',
    'users/create' => 'modules/users/create',
    'users/settings' => 'modules/users/settings',
    'users/logout(\d+)' => 'modules/users/logout',
    'users/login' => 'modules/users/login',
    'users/home' => 'modules/users/home',

    //forums
    'forums/' => 'modules/forums/index',
    'forums/viewthread/(\d+)' => 'modules/forums/viewthread/$1',
    'forums/viewforum/(\d+)' => 'modules/forums/viewforum/$1',
    'forums/viewthread/(\d+)/page-(\d+)' => 'modules/forums/viewthread/$1/page-$2',
    'forums/viewforum/(\d+)/page-(\d+)' => 'modules/forums/viewforum/$1/page-$2'

    //blog routes coming soon
    //mail message routes coming soon
    //various other routes coming soon
);


?>

I can then cycle through my $uri_routes map array and match a uri with preg_match() like this…

<?php

//get url from URL
$uri = isset($_GET['uri']) ? $_GET['uri'] : null;

//runs our function and returns an array
// $uri['module'] this will be the class/module/section
// $uri['method'] this will be the page in that section or method in that class
// $uri['urifragments'] this will either page a user ID, or an item ID or a page number for paging
$uri = get_route($_GET['uri'],$uri_routes);


function get_route($uri,$uri_routes)
{
    foreach($uri_routes as $rUri => $rRoute)
    {

        if(preg_match("#^{$rUri}$#Ui",$uri))
        {
            $uri = preg_replace("#^{$rUri}$#Ui",$rRoute,$uri);
            break;
        }
    }

    $uri = explode('/',$uri);

    $return['module'] = $uri['1'];

    $return['method'] = $uri['2'];

    $return['urifragments'] = $uri['3'];
    $return['urifragments2'] = $uri['4'];

    return $return;
}

I am open to an suggestion to improve this in any way. Right now I am stuck as there is 4 possible array key/values returned. If array key 3 or key 4 contains the word “page-” followed by a number, I would like to assign it to a $page variable. But if key 3 or key 4 contains just a number with no “page-” word, then I can assume it is a user ID, blog ID, forum ID, etc and assign it to an $id variable.

If you know a good approach to this, please help.

UPDATE
to simplify things, in addition to having “page-” in front of page numbers, I could have “id-” in front of id numbers

  • 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-19T21:54:43+00:00Added an answer on May 19, 2026 at 9:54 pm

    Instead of using $1 and $2 to match our routes try using named captures.

    5.2.2 Named subpatterns now accept the syntax (?) and (?’name’) as
    well as (?P). Previous versions
    accepted only (?P).
    Source : preg_match

    Also when you are doing a preg_replace you use \[0-99] where \0 is the whole string and \1 through \99 are the matches.

    But if you are going to be using named captures you can assign an array to the $replacement parameter with the name capture (e.g. if you capture ?P<page> then you would pass an array('page'=>"new value of page")).

    Hope that helps.

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

Sidebar

Related Questions

I'm currently working on map routing system for pedestrian pathway and not sure does
I am working with ASP.NET MVC 2 and would like to optimize my routing.
Is there any way to get truly restful routing working in MVC, just like
Morning Everyone!.. General Routing Quesiton Here... I'm currently working to achieve a route similar
I'm working on some routing functionality in an Android app, and need to access
I am working on a project, where I have used the default routing routes.MapRoute(
I have an app I'm working on at the moment with some pretty strange
I am working on a Vehicle Routing Problem. Recently I have spent a bit
I have a web application working fine in IIS6. Its using routes using System.Web.Routing.RouteCollection.
Url Routing is not working on IIS 6. (using System.Web.Routing Namespace) If i am

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.