在 PHP 开关中使用 strstr

发布于 2024-11-23 21:48:00 字数 359 浏览 4 评论 0原文

我只是想不出代码。我有太多 if 语句,我想将其更改为 switch 语句,但我找不到逻辑。

目前我有:

if(strstr($var,'texttosearch'))
   echo 'string contains texttosearch';

if(strstr($var,'texttosearch1'))
   echo 'string contains texttosearch1';

if(strstr($var,'texttosearch2'))
   echo 'string contains texttosearc2h';

//etc etc...

但是我怎样才能在交换机内实现相同的目标呢?

I just cannot think of the code. I have waay too many if statments which I want to change to be a switch statement, but I cannot find of the logic.

At the moment I have:

if(strstr($var,'texttosearch'))
   echo 'string contains texttosearch';

if(strstr($var,'texttosearch1'))
   echo 'string contains texttosearch1';

if(strstr($var,'texttosearch2'))
   echo 'string contains texttosearc2h';

//etc etc...

But how can I achieve the same within a switch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

非要怀念 2024-11-30 21:48:00
switch (true) {
  case strstr($var,'texttosearch'):
    echo 'string contains texttosearch';
    break;
  case strstr($var,'texttosearch1'):
    echo 'string contains texttosearch1';
    break;
  case strstr($var,'texttosearch2'):
    echo 'string contains texttosearc2h';
    break;
}

请注意,这与您自己的解决方案略有不同,因为如果较早的情况已经匹配,则 switch 语句不会针对其他 case 进行测试,而是因为您使用单独的 if ,而不是 if if-else 你的方式总是针对每种情况进行测试。

switch (true) {
  case strstr($var,'texttosearch'):
    echo 'string contains texttosearch';
    break;
  case strstr($var,'texttosearch1'):
    echo 'string contains texttosearch1';
    break;
  case strstr($var,'texttosearch2'):
    echo 'string contains texttosearc2h';
    break;
}

Note, that this is slightly different to your own solution, because the switch-statement will not test against the other cases, if an earlier already matches, but because you use separate ifs, instead if if-else your way always tests against every case.

初见终念 2024-11-30 21:48:00

我认为你无法使用 switch (比现在更优雅)来实现这一点,因为它比较值,但你只想比较部分值。相反,你可以使用循环:

$patterns = array('texttosearch', 'texttosearch1', 'texttosearch2');
foreach ($patterns as $pattern) {
    if (strstr($var, $pattern)) {
        echo "String contains '$pattern'\n";
    }
}

I think you can't achieve this with switch (more elegant than now) because it compare values but you want compare only part of values. Instead you may use loop:

$patterns = array('texttosearch', 'texttosearch1', 'texttosearch2');
foreach ($patterns as $pattern) {
    if (strstr($var, $pattern)) {
        echo "String contains '$pattern'\n";
    }
}
橘亓 2024-11-30 21:48:00

你也可以用相反的方式来做:

switch(true) {
case strstr($var, "texttosearch"):
    // do stuff
    break;
case strstr($var, "texttosearch1"):
    // do other stuff
    break;
}

You can do it the other way around:

switch(true) {
case strstr($var, "texttosearch"):
    // do stuff
    break;
case strstr($var, "texttosearch1"):
    // do other stuff
    break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文