开关盒问题
如何修复它或添加正则表达式?
case substr($rrr['url'],-4)=='.jpg' || '.png' || '.gif' || '.tif' || '.tiff':
how can fix it or add regexp ?
case substr($rrr['url'],-4)=='.jpg' || '.png' || '.gif' || '.tif' || '.tiff':
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西吗?
请注意,我在
case
表达式之间省略了break;
。也很酷:
问题中的片段不起作用,因为它被评估为(缩短)。
这有效,但显然它没有多大意义,而且很可能不是所期望的。
更新:这个解决方案看起来更干净。它假设
$rrr['url']
是这里唯一有趣的。查看评论Something like this?
Note, that I omit
break;
between thecase
-expression.Also cool:
The snippet from the question doesnt work, because its evaluated into (shortened)
This works, but oviously it doesnt make much sense and is very probably not, what is expected.
Update: This solution seems much cleaner. It assumes, that
$rrr['url']
is the only interesting here. See comments$foo == A ||乙|| C
不起作用,这需要是$foo == A || $foo == B || $foo == C
或in_array($foo, array(A, B, C))
。switch
语句中不能有复杂的case
。每种情况只能有一个值来与比较值进行比较。您必须将其编写为单独的失败案例:$foo == A || B || C
doesn't work, this needs to be$foo == A || $foo == B || $foo == C
orin_array($foo, array(A, B, C))
.You can't have complex
case
s withinswitch
statements. Each case can only have one value against which the comparison value will be compared. You'd have to write this as separate fall-throughcase
s:你不能使用
A == B || C|| D
语句,只有A == B || A == C || A == D
另外,url 可以有 GET 参数。
You cant use
A == B || C || D
statement for that, onlyA == B || A == C || A == D
Also, urls can have GET parameters.
您需要找到点的 strrpos ,获取最右边的点位置之后的子字符串(返回通过 strrpos),定义允许的扩展数组(这也使您的代码可重用),然后使用 in_array 像这样:
You need to find strrpos of dot, get substring after rightest dot position (which returned by strrpos), define array of allowed extensions (it also makes your code reusable), and then use in_array like this: