How to make regex variables to capture of routes

Posted by Zephiro on Stack Overflow See other posts from Stack Overflow or by Zephiro
Published on 2011-11-12T01:36:43Z Indexed on 2011/11/12 1:51 UTC
Read the original article Hit count: 410

Filed under:
|
|
|
|

anyone can help me with this problem.

example $uri = '/username/carlos'; => $routes[] = '/username/@name';

@name convert in variable $name capturing string "carlos"

$routes[] = '/list/edit/@id:[0-9]{3}';

$routes[] = '/username/@name';

$routes[] = '/archive/*';

$routes[] = '/';


$uri = '/username/carlos';

foreach ( $routes as $pattern )
{
    if ( preg_match( '#^' . preg_replace( '#(?:{{)?@(\w+\b)(?:}})?#i', '(?P<\1>[\w\-\.!~\*\'"(),\s]+)',
                            str_replace( '\*', '(.*)', preg_quote( $pattern, '/' ) ) ) . '\/?$#i', $uri, $matchs ) )
    {

        //how to make regex for this to work :


        echo $name; // carlos =>$uri = '/username/carlos'; or matt => $uri = '/username/matt';

    }
}

thanks for reading

© Stack Overflow or respective owner

Related posts about php

Related posts about regex