开关盒问题

发布于 2024-10-30 08:46:40 字数 126 浏览 4 评论 0原文

如何修复它或添加正则表达式?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2024-11-06 08:46:40

像这样的东西吗?

case in_array(substr($rrr['url'],-4), array('.jpg','.png','.gif','.tif')):
case in_array(substr($rrr['url'],-5), array('.tiff')):

请注意,我在 case 表达式之间省略了 break;

也很酷:

case in_array(pathinfo($rrr['url'], PATHINFO_EXTENSION), array('jpg','png','gif','tif', 'tiff')):

问题中的片段不起作用,因为它被评估为(缩短)。

(substr($rrr['url'],-4)=='.jpg') || '.png'

这有效,但显然它没有多大意义,而且很可能不是所期望的。

更新:这个解决方案看起来更干净。它假设 $rrr['url'] 是这里唯一有趣的。查看评论

switch (pathinfo($rrr['url'], PATHINFO_EXTENSION)):
  case 'jpg':
  case 'png':
  case 'gif':
  case 'tif':
  case 'tiff':
    do_something();
  break;
}

Something like this?

case in_array(substr($rrr['url'],-4), array('.jpg','.png','.gif','.tif')):
case in_array(substr($rrr['url'],-5), array('.tiff')):

Note, that I omit break; between the case-expression.

Also cool:

case in_array(pathinfo($rrr['url'], PATHINFO_EXTENSION), array('jpg','png','gif','tif', 'tiff')):

The snippet from the question doesnt work, because its evaluated into (shortened)

(substr($rrr['url'],-4)=='.jpg') || '.png'

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

switch (pathinfo($rrr['url'], PATHINFO_EXTENSION)):
  case 'jpg':
  case 'png':
  case 'gif':
  case 'tif':
  case 'tiff':
    do_something();
  break;
}
南城追梦 2024-11-06 08:46:40
  1. $foo == A ||乙|| C 不起作用,这需要是 $foo == A || $foo == B || $foo == Cin_array($foo, array(A, B, C))
  2. switch 语句中不能有复杂的 case。每种情况只能有一个值来与比较值进行比较。您必须将其编写为单独的失败案例:

    开关 ($foo) {
        情况一:
        情况B:
        情况C:
            酒吧();
            休息;
    }
    
  1. $foo == A || B || C doesn't work, this needs to be $foo == A || $foo == B || $foo == C or in_array($foo, array(A, B, C)).
  2. You can't have complex cases within switch 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-through cases:

    switch ($foo) {
        case A :
        case B :
        case C :
            bar();
            break;
    }
    
狼性发作 2024-11-06 08:46:40

你不能使用 A == B || C|| D 语句,只有 A == B || A == C || A == D

另外,url 可以有 GET 参数。

You cant use A == B || C || D statement for that, only A == B || A == C || A == D

Also, urls can have GET parameters.

烟雨凡馨 2024-11-06 08:46:40

您需要找到点的 strrpos ,获取最右边的点位置之后的子字符串(返回通过 strrpos),定义允许的扩展数组(这也使您的代码可重用),然后使用 in_array 像这样:

$rpos = strrpos($rrr['url'], '.');
$ext = substr($rrr['url'], $rpos+1);
$allowedExtensions = array('jpg','png', 'gif', 'tif', 'tiff');
///....
if (in_array($ext, $allowedExtensions)) {
///....

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:

$rpos = strrpos($rrr['url'], '.');
$ext = substr($rrr['url'], $rpos+1);
$allowedExtensions = array('jpg','png', 'gif', 'tif', 'tiff');
///....
if (in_array($ext, $allowedExtensions)) {
///....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文