PHP在字符串中搜索单词


php search within string for word?

我使用PHP的preg_match来帮助确定字符串的值。
但是代码只输出1或2。

为什么我的if语句的最后两个案例不匹配?

$atype = strtolower($userData['user_type']);   // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" || 
    (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
       $atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||         
          (preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
       $atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" || 
          (preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
       $atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" || 
          (preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
       $atype = 4;
}
echo $atype;

你需要用更好的方式解释你的问题。

但我猜你说的数据假设是company introducer

所以它已经匹配了第一个if块的条件。例:

在公司介绍人preg_match将返回true。

if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype))  ){
    $atype =1 ;
}