Skip to content

Instantly share code, notes, and snippets.

@arjaneising
Created January 7, 2012 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arjaneising/1574954 to your computer and use it in GitHub Desktop.
Save arjaneising/1574954 to your computer and use it in GitHub Desktop.
Routed URL helper for CodeIgniter
<?php
function route_url($segments, $slash = true) {
$CI =& get_instance();
$routes = $CI->router->routes;
$toReturn = false;
foreach ($routes as $route => $arch) {
$arch = str_replace('/', '\/', $arch);
$arch = str_replace(array('$1', '$2', '$3', '$4'), '([a-zA-Z0-9\-_]+)', $arch);
if (preg_match('/^' . $arch . '$/', $segments, $matches)) {
$toReturn = $route;
if (isset($matches[1])) {
for ($i = 1; $i < count($matches); ++$i) {
$toReturn = preg_replace('/(\(:any\))/', $matches[$i], $toReturn, 1);
}
}
break;
}
}
if ($toReturn) {
return ($slash ? '/' : '') . $toReturn;
}
return ($slash ? '/' : '') . $segments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment