How to match numbers in an array in PHP

2019-05-30 04:30发布

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条回答
虎瘦雄心在
2楼-- · 2019-05-30 05:12

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.

查看更多
登录 后发表回答