I’m creating a simple PHP routing class.
I want to check REQUEST_URI for RegEx patterns, for example:
foreach($routes AS $pattern) {
$captures = NULL;
if(preg_match($uri, $pattern, $captures)) { /* do something */ }
}
But if I’ll have too much routes this code will check them too long.
Is there more fast method for doing this?
Thank you.
No, you have to loop over them.
The bigger question is why do you think “the code will check them too long”? Have you tried it? How many routes do you have? This sounds like premature optimization, where you’re concerned about making things faster when you actually have no idea if it’s not fast enough.
Try it with a lot of patterns and see how long it actually takes. If it’s a problem, then it’s worth pursuing. Otherwise, it’s just a waste of your time.