Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function route_path_match(string $route, string $path): bool {
- // split the defined route into its parts
- $routeParts = explode('/', $route);
- // split the called url into its parts
- $pathParts = explode('/', explode('?', $path)[0]);
- // get the number of parts of the route and the path
- $routePartsCount = count($routeParts);
- $pathPartsCount = count($pathParts);
- // get the maximum part count
- $limit = max($routePartsCount, $pathPartsCount);
- for ($i = 0; $i < $limit; ++$i) {
- // if there are no more elements in either array, return false
- if ($i >= $pathPartsCount || $i >= $routePartsCount) return false;
- // if one of the elements is a wildcard, return true
- if ($routeParts[$i] === '**') return true;
- // if the current route part is a parameter, skip it
- if (strpos($routeParts[$i], ':') === 0) continue;
- // if the route and the path do not match at one point, return false
- if ($pathParts[$i] !== $routeParts[$i]) return false;
- }
- // if the loop is completed, the path matches the given route
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement