在strstr中使用多个值($ _ SERVER ['PHP_SELF'] ...)

时间:2012-10-24 22:40:00

标签: php

另一个让我陷入困境的简单事情:

我正在使用以下内容检查当前网址并根据结果选择一个div类:

$checkit = $_SERVER['PHP_SELF'];
... 
<li "; if(strstr($checkit,'welcome')) { echo "class='active_tab'"; }...

我想要做的还是检查网址是否包含其他单词,这些单词也需要同一个'li'项目被赋予'active_tab'类,但我无法弄清楚格式。像这样的东西,虽然显然这不起作用:

<li "; if(strstr($checkit,'welcome', 'home', 'yourprofile')) { echo "class='active_tab'"; }...

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

知道这是一种更好的方法,但是会有间隙修复:

$searchStrings = array('welcome','home','yourprofile');
$stringFound = false;
foreach($searchStrings as $checkString)
{
  if(strstr($checkit, $checkString))
  {
    $stringFound = true;
    break;
  }
}

然后使用$stringFound更改输出。

编辑1:为continue切换break感谢ZombieHunter(已经晚了-_-)

编辑2:或者你可以使用正则表达式(虽然我觉得这里有点过分)

if(preg_match('/(welcome|home|your profile)/',$checkit))
{
 // Do your stuff here
}

但是这不是那么富有表现力(更容易阅读和扩展数组),如果这些值开始堆积,它更容易将数组挂钩到像DB查询这样的存储器中。

相关问题