Created
January 7, 2012 14:58
-
-
Save arjaneising/1574954 to your computer and use it in GitHub Desktop.
Routed URL helper for CodeIgniter
This file contains 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 | |
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