Advertisement
Guest User

Untitled

a guest
Apr 21st, 2011
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. $routes     =   array();
  2.    
  3.     $routes[]   =   array(":controller/:action", array(
  4.                                                 "controller" => '',
  5.                                                 'action' => ''));
  6.    
  7.    
  8.     $routes[]   =   array(":controller/:action/(\d+)", array(
  9.                                                 "controller" => '',
  10.                                                 'action' => '',
  11.                                                 'id' => 0));
  12.    
  13.     $routes[]   =   array(":controller/show/(\d+)", array(
  14.                                                 "controller" => '',
  15.                                                 'action' => 'show',
  16.                                                 'id' => 0));
  17.    
  18.     $routes[]   =   array(":controller/show/(\d+)/:name", array(
  19.                                                 "controller" => '',
  20.                                                 'action' => 'show',
  21.                                                 'id' => 0,
  22.                                                 'name' => ''));
  23.    
  24.     //test URLs
  25.     $urlCnt[0]  =   "/information/location";
  26.     $urlCnt[1]  =   "/webdesign/show/1";
  27.     $urlCnt[2]  =   "/information/show/3/why-you-should-choose-us";
  28.     $urlCnt[3]  =   "/information/delete/3/becauseItsOld";
  29.    
  30.     //some empty stuff for later
  31.     $params     =   array();
  32.     $regexContainer =   array();
  33.     $regex      =   "";
  34.    
  35.     //prepare the regex to parse the urls
  36.     foreach($routes as $route)
  37.     {  
  38.         $ga =   preg_split("/\//", $route[0] );
  39.         foreach($ga as $expr)
  40.         {
  41.             //check if the name starts with a : - if so than it will be a variable that is needed as an array index
  42.             //important for later stuff, as controller, action, params always be there as an index
  43.             if( strpos($expr, ":") === 0 )
  44.             {
  45.                 //:controller or :action found or whatever -> a regex need to be used
  46.                 $regex  .= "\/[a-zA-Z-0-9]+";
  47.             }
  48.             else
  49.             {
  50.                 //not found therefore what is written is the requirement
  51.                 $regex  .= "\/" . $expr;               
  52.             }
  53.            
  54.         }  
  55.         $regexContainer[]   = $regex;
  56.         $regex =    null;
  57.     }
  58.    
  59.    
  60.     //matches
  61.     foreach($regexContainer as $indiRegex)
  62.     {
  63.         print "<h1>" . $indiRegex . "</h1>";
  64.         foreach ($urlCnt as $url)
  65.         {          
  66.              if ( preg_match("/^" . $indiRegex . "\z/i", $url ) === 1  )
  67.              {
  68.                 print $url . "<br />";
  69.              }
  70.              else
  71.              {             
  72.                 print "NO:( <br />";
  73.              }
  74.         }
  75.         print "<br />";
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement