Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Regex to match url for route function. Can't get Regex to work
    primarykey
    data
    text
    <p>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. </p> <p>Basically, I am trying to get the 'admin/stats/(:num)' entry to match on both 'admin/stats' and admin/stats/1'.</p> <p>While the script is starting I grab all paths from a separate array and use a foreach to save each path: </p> <pre><code>route('admin/stats/(:num)', array('#title' =&gt; 'Statistics',...)); </code></pre> <p>The function is:</p> <pre><code>function route($path = NULL, $options = NULL) { static $routes; //If no arguments are supplied, return all routes stored. if(!isset($path) &amp;&amp; !isset($options)) { return $routes; } //return options for path if $path is set. if(isset($path) &amp;&amp; !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; } </code></pre> <p>Route replace function:</p> <pre><code>function route_replace($path) { return str_replace(':any', '.+', str_replace(':num', '[0-9]+', $path)); } </code></pre> <p>A key/value pair in the $routes array looks like:</p> <pre><code>[admin/stats/(:num)] =&gt; Array ( [#title] =&gt; Statistics //Page title [#access] =&gt; user_access //function to check if user is authorized [#content] =&gt; html_stats //function that returns HTML for the page [#form_submit] =&gt; form_stats //Function to handle POST submits. ) </code></pre> <p>Thanks for the help. This is my first router and I am not that familiar in making proper Regex's. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload