Skip to content

Instantly share code, notes, and snippets.

@fivestar
Created June 10, 2010 05:04
Show Gist options
  • Save fivestar/432577 to your computer and use it in GitHub Desktop.
Save fivestar/432577 to your computer and use it in GitHub Desktop.
<?php
$str1 = "/room/word:yudoufu/color:pink";
$str2 = "/room/word:yudoufu";
$str3 = "/room/color:pink";
$pattern = "#^/room(?P<params>(?:/(?:word|color)+:[^/]+)+)$#";
if (preg_match($pattern, $str1, $m)) var_dump($parameterString = $m['params']);
if (preg_match($pattern, $str2, $m)) var_dump($m['params']);
if (preg_match($pattern, $str3, $m)) var_dump($m['params']);
// string(24) "/word:yudoufu/color:pink"
// string(13) "/word:yudoufu"
// string(11) "/color:pink"
$params = array();
foreach (explode('/', trim($parameterString, '/')) as $keyValue) {
list($k, $v) = explode(':', $keyValue);
$params[$k] = $v;
}
var_dump($params);
// array(2) {
// ["word"]=>
// string(7) "yudoufu"
// ["color"]=>
// string(4) "pink"
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment