fork(1) download
  1. <?php
  2.  
  3. $r="user/username/{name}/id/{id}/age/{age}/profile";
  4.  
  5. $u="user/username/joe/id/99/age/33/profile";
  6.  
  7. route::match($r, $u);
  8. class route{
  9.  
  10.  
  11.  
  12. public static function match($route, $url)
  13. {
  14. if(strpos($route, '{')===FALSE)
  15. {
  16. if(strcmp($route, $url)==0)
  17. {
  18. return TRUE;
  19. }
  20. return FALSE;
  21. }
  22. $vars=[];
  23. $umatches=[];
  24.  
  25. preg_match_all('/\{(.*?)\}/', $route, $matches);
  26.  
  27. preg_match('/(.*?)\{/', $route, $amatches);
  28.  
  29. preg_match_all('/\}(.*?)\{/', $route, $bmatches);
  30.  
  31. $a=preg_split(end($bmatches[1]), $route);
  32.  
  33. $b=preg_split('/\}(.*?)/', $a[1]);
  34.  
  35. array_push($umatches, $amatches[1]);
  36.  
  37. foreach ($bmatches[1] as $key => $value)
  38. {
  39. array_push($umatches, $value);
  40. }
  41.  
  42. array_push($umatches, $b[1]);
  43.  
  44. $pattern="/".str_replace('/', '\/', $amatches[1])."/";
  45.  
  46. $split=preg_split($pattern, $url);
  47.  
  48. $i=0;
  49.  
  50. foreach ($umatches as $key => $value) {
  51. $value=str_replace('/', '\/', $value);
  52. $value='/'.$value.'/';
  53. $r=preg_split($value, $url);
  54. $url=$r[1];
  55. if($i>0)array_push($vars, $r[0]);
  56. $i++;
  57. }
  58. print_r($vars);
  59.  
  60. if(sizeof($matches[1])!=sizeof($vars)) return FALSE;
  61.  
  62. $params=[];
  63.  
  64. for ($i=0; $i < sizeof($matches[1]); $i++) {
  65. $params[$matches[1][$i]]=$vars[$i];
  66. }
  67.  
  68. print_r($params);
  69. return $params;
  70. }
  71. }
Success #stdin #stdout #stderr 0.03s 52480KB
stdin
Standard input is empty
stdout
Array
(
    [0] => joe
    [1] => 99
    [2] => 
)
Array
(
    [name] => joe
    [id] => 99
    [age] => 
)
stderr
PHP Notice:  Undefined offset: 1 in /home/TlNsKr/prog.php on line 42