Advertisement
joernneumeyer

Untitled

Jan 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. <?php
  2.   function route_path_match(string $route, string $path): bool {
  3.     // split the defined route into its parts
  4.     $routeParts = explode('/', $route);
  5.     // split the called url into its parts
  6.     $pathParts = explode('/', explode('?', $path)[0]);
  7.     // get the number of parts of the route and the path
  8.     $routePartsCount = count($routeParts);
  9.     $pathPartsCount = count($pathParts);
  10.     // get the maximum part count
  11.     $limit = max($routePartsCount, $pathPartsCount);
  12.     for ($i = 0; $i < $limit; ++$i) {
  13.       // if there are no more elements in either array, return false
  14.       if ($i >= $pathPartsCount || $i >= $routePartsCount) return false;
  15.       // if one of the elements is a wildcard, return true
  16.       if ($routeParts[$i] === '**') return true;
  17.       // if the current route part is a parameter, skip it
  18.       if (strpos($routeParts[$i], ':') === 0) continue;
  19.       // if the route and the path do not match at one point, return false
  20.       if ($pathParts[$i] !== $routeParts[$i]) return false;
  21.     }
  22.     // if the loop is completed, the path matches the given route
  23.     return true;
  24.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement