-
-
Save jongacnik/76631a2acf676e1ee9c6c11459f26507 to your computer and use it in GitHub Desktop.
php-regex-router.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// dangerously simple PHP regular expression URL router | |
// requires a mod_rewrite like "RewriteRule . /index.php [L]" | |
function get($url, $callback) { | |
$matches = array(); | |
if (preg_match('~' . $url . '~', $_SERVER['REQUEST_URI'], $matches)) { | |
echo call_user_func_array($callback, $matches); | |
die(); | |
} | |
} | |
get('foo', function($url) { | |
return 'you got foo'; | |
}); | |
get('bar([\d])', function($url, $digit) { | |
return 'bar number ' . $digit; | |
}); | |
get('.*', function() { | |
return 'catch all. try /foo or /bar[0-9]'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment