I'm currently building a CMS for my projects and I'm trying to route to my 'Admin' controller. N开发者_JAVA技巧ow my regexp is:
'admin(.*)'
But with this everything that start with 'admin' will be matched. I would like to match the route when 'admin', 'admin/foo' and 'admin/foo/bar' etc, is present. Not when 'adminn' or something like that.
Hope you understand what I'm after. Thx / Tobias
Alter the regex like this:
admin(\/.*)?
For string, such as admin/foo/bar/salami/pizza
, how about preg_match('~^admin\/~', $string)
?
Two approaches could work without regular expressions at all
- Search for the longest substring first, that way you won't match admin first. So, with your examples, check for admin/foo/bar first, then admin/foo, then admin.
- explode() the string on the slash, then work your way down the bits to figure out what matches, e.g. explode('/', $request) then check the parts; if the first result is admin, pass it to the admin controller, which can figure out the route from the rest.
Another approach:
$sample_string="admin/foo/bar";
if(in_array("admin", explode("/",$sample_string))){
//true
}else{
//false
}
Or you could even replace the explode "/" with explode by "admin".....
精彩评论