如何在 PHP 中匹配数组中的数字 [英] How to match numbers in an array in PHP

查看:33
本文介绍了如何在 PHP 中匹配数组中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的 PHP 应用程序中的路由或 uri.目前我有一个带有正则表达式 => 这样的 url 映射的数组...

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
);


?>

然后我可以循环遍历我的 $uri_routes 映射数组并像这样将 uri 与 preg_match() 匹配...

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;
}

<小时>

我愿意接受以任何方式改进这一点的建议.现在我被卡住了,因为有 4 个可能的数组键/值返回.如果数组键 3 或键 4 包含单词page-"后跟一个数字,我想将其分配给 $page 变量.但是如果键 3 或键 4 只包含一个没有page-"字样的数字,那么我可以假设它是用户 ID、博客 ID、论坛 ID 等,并将其分配给 $id 变量.


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.

更新
为了简化事情,除了在页码前面有page-"之外,我还可以在id号前面有id-"

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 来匹配我们的路由,请尝试使用命名捕获.

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

5.2.2 命名子模式现在接受语法 (?) 和 (?'name') 作为以及(?P).之前的版本仅接受 (?P).来源:preg_match

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

此外,当您执行 preg_replace 时,您使用 \[0-99] 其中 \0 是整个字符串,\1 到 \99 是匹配项.

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.

但是如果您要使用命名捕获,您可以使用名称捕获将数组分配给 $replacement 参数(例如,如果您捕获 ?P<page> 然后你会传递一个 array('page'=>"new value of page")).

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")).

希望有所帮助.

这篇关于如何在 PHP 中匹配数组中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆