Advertisement
aleiphoenix

uri_mapping

Jan 10th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2.  
  3. $str = "/parent/:dbname/:collname/:id";
  4.  
  5. function uri2regex ($str) {
  6.     $word = "([0-9a-zA-Z_-]+)";
  7.     $flag = preg_match_all("/:\w+/",$str, $matches);
  8.     $params = $matches[0];
  9.     array_walk($params, 'uriNormalize');
  10.     var_dump($params);
  11.     $ret = preg_replace("/:\w+/", $word, $str);
  12.     $ret = str_replace("/", "\/", $ret);
  13.     return "/^" . $ret . "/";
  14. }
  15.  
  16. function uriNormalize (&$item) {
  17.     $item = str_replace(":", "", $item);
  18. }
  19.  
  20. $regex = uri2regex($str);
  21.  
  22. var_dump($regex);
  23.  
  24. $uri = "/parent/foo_db/bar_coll/200";
  25.  
  26. $flag = preg_match_all($regex, $uri, $matches);
  27.  
  28. var_dump($flag);
  29.  
  30. var_dump($matches);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement