在 PHP 中使用正则表达式作为请求处理程序是否不好?
我正在开发一个请求处理程序,通过我的索引页面路由每个页面调用,并具有 SEO 友好的 URL。
domain.com/account/settings
这很容易映射到正确的页面,但当 uri 中出现 ID 号或页码时,有些会变得更复杂。
所以我看到有些人会使用 preg_match 之类的东西并循环遍历一系列模式 -> uri 来获得匹配,这对于分页和 id 发挥作用时非常有用,但根据我的经验,似乎在每个页面加载时对包含 20 个项目的数组运行 preg_match 对性能不利。
请告诉我你对此的想法?
I am working on a request handler to route every page call through my index page and have SEO friendly urls.
domain.com/account/settings
this would be easy to map to the correct page but some are more complex when there becomes an ID number or a page number in the uri.
So I see some people will use things like preg_match and cycle through an array of patterns -> uri to get a match which is nice for when paging and id's come into play but from my experience, it seems like running preg_match on an array of say 20 items on every page load is not good for performace.
Please tell me your thought on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于像 URI 这样短的字符串,不太复杂的正则表达式将花费很少的时间,甚至运行 20 次。如果你a)对其进行分析或计时以证明这是一个性能问题,并且b)有一个很好的替代方案可以使用,那么你可以尝试改变它,但否则我不会太担心它。毕竟,大量网站都使用 mod_rewrite 执行类似的操作,在每次页面加载时根据一系列正则表达式检查页面 URI。
如果需要,您可以通过一些简单的 strstr() 检查来查看 URI 的基本格式(是否包含 id、页码等),从而将每个 URI 的数量减少 20 倍甚至更少。 。优化您的正则表达式,例如尽可能使用“start”
^
和“end”$
元字符,也会有所帮助。A not-very-complex regex on a string as short as an URI will take very little time, even running 20 times through. If you have a) profiled or timed it to prove it's a performance problem, and b) a good alternative to use instead then you could try changing it around but otherwise I wouldn't worry to much about it. Tons of sites do something similar with mod_rewrite after all, checking the page URI against a series of regexes on every page load.
If need be, you could probably reduce that 20 times to less for each URI with a few simple strstr() checks to see what basic format the URI is in (whether it contains a id or not, a page number or not, etc). Optimizing your regexes, such as using the "start"
^
and "end"$
meta-characters wherever possible, will help too.这是你应该关注的事情:什么是更好的选择,而不是这个看起来很糟糕。如果你有更好的替代方案,他们就会使用它。如果没有,请使用正则表达式解决方案,直到您知道需要加快速度(我将省略我通常对过早优化的抱怨)。
我个人会使用正则表达式处理程序。与此问题的其他替代方案相比,它们更灵活、更容易且更易于维护。但是YMMV...
Here's the thing you should be looking at: What's the better alternative, not that this one seems bad. If you have an alternative that seems better, they use it. If not, use the regex solution until you know you need to speed it up (I'll leave out my usual rant about premature optimization).
I would use the regex handlers personally. They are more flexible, easier, and easier to maintain than some other alternatives to this problem. But YMMV...
在我的网站上,我只有一些不同的重写规则,我认为效果很好:
On my site I just have a few different rewrite rules, which I think works well:
我认为,这样的做法是可以的。例如,Drupal 和 Django 框架正是这样做的。
另一种替代方法是使用 URL 重写引擎(例如,请参阅此问题了解详细信息)。
I think, such approach is OK. For example, Drupal and Django frameworks do exactly that.
Another alternative is using URL Rewrite Engine (see, for example, this question for details).