3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Parses routes of the following form: * * "/user/{name}/{id:[0-9]+}" */ class Std { const VARIABLE_REGEX = <<<'REGEX' ~\{ \s* ([a-zA-Z][a-zA-Z0-9_]*) \s* (?: : \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*) )? \}~x REGEX; const DEFAULT_DISPATCH_REGEX = '[^/]+'; public function parse($route) { if (!preg_match_all( self::VARIABLE_REGEX, $route, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER )) { return [$route]; } $offset = 0; $routeData = []; foreach ($matches as $set) { if ($set[0][1] > $offset) { $routeData[] = substr($route, $offset, $set[0][1] - $offset); } $routeData[] = [ $set[1][0], isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX ]; $offset = $set[0][1] + strlen($set[0][0]); } if ($offset != strlen($route)) { $routeData[] = substr($route, $offset); } return $routeData; } } $Parser = new Std; var_dump( $Parser->parse("/user/{name}/{id:[0-9]+}/franchise") );
Output for git.master, git.master_jit, rfc.property-hooks
array(5) { [0]=> string(6) "/user/" [1]=> array(2) { [0]=> string(4) "name" [1]=> string(5) "[^/]+" } [2]=> string(1) "/" [3]=> array(2) { [0]=> string(2) "id" [1]=> string(6) "[0-9]+" } [4]=> string(10) "/franchise" }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
48.59 ms | 401 KiB | 8 Q