PHP if/switch 由 * 连接
我想知道是否有人知道是否有一种方法可以将 * (全部)连接到 if (或 switch)语句内的字符串。例如,如果您有一个名为 /hello/there 和 /hello/whats-up ... 的 URL,那么您是否可以有类似以下内容的内容:
if ($url="/hello/" . *) {
sayHello();
} else { sayGoodebye(); }
等等... 我认为这不是正确的语法,但如果有人知道我在说什么这将是一个很大的帮助。
谢谢 (:
I was wondering if anybody knew if there was a way to concatenate a * (all) to a string inside an if (or switch) statement. For example if you had a URL called /hello/there and /hello/whats-up ... is there anyway you could have something like the following:
if ($url="/hello/" . *) {
sayHello();
} else { sayGoodebye(); }
etc... I don't think that's the correct syntax, but if anybody knows what I'm talking about it would be a great help.
Thanks (:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不需要,请不要使用正则表达式...
Do not use regular expressions if you don't have to...
您还可以检查 $match 在 $url 字符串中的位置:
You can also check for the position of $match in the $url string:
使用正则表达式:
http://www.regular-expressions.info/php.html
所以它是像这样的东西:
虽然这会匹配任何地方带有“/hello/”的任何内容,所以如果您只想匹配以“/hello/”开头的字符串,则必须修改表达式。关键是,如果您从未使用过正则表达式,那么投入一些时间是一件好事。它最终会得到回报,因为在某些时候你会需要这项技能。
编辑:我将在这里留下原始代码作为参考,但请参阅 phihag 的注释并使用 preg_match 和 preg_match 兼容表达式而不是 ereg 和我使用的表达式。
Use regular expressions:
http://www.regular-expressions.info/php.html
So it would be something like this:
Although that would match anything with "/hello/" in it anywhere, so if you only wanted to match strings that start with "/hello/" you'd have to modify the expression. The point is, if you've never used regular expressions it's a good thing to invest some time into. It'll pay off eventually because at some point you're going to need this skill.
Edit: I'll leave my original code here as reference, but please see phihag's comments and use preg_match and the preg_match compatible expression instead of ereg and the expression I used.