3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php <?php class Route { const RESOURCE_PATTERN = '/'.'\[:(.*)\]'.'/'; const TOKEN_PARA_PATTERN = '/'.'\{:(.*)\}'.'/'; private $methodList; private $url; private $urlTokenList = array(); private $urlTokenCount; private $controller; private $action; private $tokenParameterList = array(); private $tokenParameterDetailList = array(); private $resourceList = array(); private $resourceDetailList = array('music_recording' => array('jazz', 'pop')); private $requestURL; private $requestURLTokenList; private $requestURLTokenCount; public function __construct($httpMethod, $httpUrl, $controller, $action) { $this->methodList = explode('|', $httpMethod); $this->url = $httpUrl; $this->controller = $controller; $this->action = $action; $this->parseURL($httpUrl); } public function parseURL($url) { $tmpUrl = $url; if (substr($tmpUrl, 0, 1) == '/') { $tmpUrl = substr($tmpUrl, 1); } if (substr($tmpUrl, -1) == '/') { $tmpUrl = substr($tmpUrl, 0, -1); } $tmpURLTokenList = explode('/', $tmpUrl); $this->urlTokenCount = count($tmpURLTokenList); foreach($tmpURLTokenList as $token) { $tokenInfo = array(); $tokenInfo['name'] = $token; $isMatchedForResourcePattern = preg_match(self::RESOURCE_PATTERN, $token, $resourceMatches, PREG_OFFSET_CAPTURE); if ($isMatchedForResourcePattern === 1) { array_push($this->resourceList, $resourceMatches[1][0]); $tokenInfo['isResource'] = 1; $tokenInfo['resourceName'] = $resourceMatches[1][0]; } else { $tokenInfo['isResource'] = 0; } $isMatchedForParaPattern = preg_match(self::TOKEN_PARA_PATTERN, $token, $paramMatches, PREG_OFFSET_CAPTURE); if ($isMatchedForParaPattern === 1) { array_push($this->tokenParameterList, $paramMatches[1][0]); $tokenInfo['isParam'] = 1; $tokenInfo['paramName'] = $paramMatches[1][0]; } else { $tokenInfo['isParam'] = 0; } array_push($this->urlTokenList, $tokenInfo); } } public function parseRequestURL($url) { $tmpUrl = $url; if (substr($tmpUrl, 0, 1) == '/') { $tmpUrl = substr($tmpUrl, 1); } if (substr($tmpUrl, -1) == '/') { $tmpUrl = substr($tmpUrl, 0, -1); } $this->requestURLTokenList = explode('/', $tmpUrl); $this->requestURLTokenCount = count($this->requestURLTokenList); } public function getMethodList() { return $this->methodList; } public function getUrl() { return $this->url; } public function getUrlTokenList() { return $this->urlTokenList; } public function getUrlTokenCount() { return $this->urlTokenCount; } public function getController() { return $this->controller; } public function getAction() { return $this->action; } public function getTokenParameterList() { return $this->tokenParameterList; } public function getResourceList() { return $this->resourceList; } public function getResourceDetailList() { return $this->resourceDetailList; } public function isMatched($url) { $this->requestURL = $url; $this->parseRequestURL($url); // echo "count not the same[".$this->requestURLTokenCount." - ".$this->urlTokenCount."]"; if ($this->requestURLTokenCount !== $this->urlTokenCount) { // echo "count not the same[".$this->requestURLTokenCount." - ".$this->urlTokenCount."]"; return 0; } $isMatched = 1; for($i=0;$i<($this->urlTokenCount);$i++) { $urlToken = $this->urlTokenList[$i]; $requestURLToken = $this->requestURLTokenList[$i]; if ($urlToken['isResource'] !== 1 && $urlToken['isParam'] !== 1) { if (strcmp($urlToken['name'], $requestURLToken)) { $isMatched = 0; break; } } else if ($urlToken['isResource'] === 1) { if (array_key_exists($requestURLToken, $this->resourceDetailList) && in_array($requestURLToken, $this->resourceDetailList[$urlToken['resourceName']])) { $isMatched = 1; } else { $isMatched = 0; break; } } else if ($urlToken['isParam'] === 1) { $this->tokenParameterDetailList[$urlToken['paramName']] = $requestURLToken; } } return $isMatched; } public function getControllerInstance() { } } $ROUTES = array( array('httpMethod' => 'GET', 'url' => '[:music_category]/xml', 'controller' => 'CategoryController', 'action' => 'xml'), array('httpMethod' => 'GET', 'url' => '[:music_category]/json', 'controller' => 'CategoryController', 'action' => 'json'), array('httpMethod' => 'GET|POST', 'url' => 'xml', 'controller' => 'temp_controller', 'action' => 'xml'), array('httpMethod' => 'GET|POST', 'url' => 'json', 'controller' => 'temp_controller', 'action' => 'json'), array('httpMethod' => 'GET|POST', 'url' => 'post/[:music_category]/{:data}/xml', 'controller' => 'CategoryController', 'action' => 'post') ); $routeObjArray = array(); foreach ($ROUTES as $route) { array_push($GLOBALS['routeObjArray'], new Route($route['httpMethod'], $route['url'], $route['controller'], $route['action'])); } $testURl = "music/xml"; foreach ($routeObjArray as $abc) { print_r ($abc->isMatched($testURl) == 1 ? 'true' : 'false'); }
based on TTv4k
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.4.34
Parse error: syntax error, unexpected '<' in /in/aCiSM on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '<' in /in/aCiSM on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/aCiSM on line 3
Process exited with code 255.