为什么preg_match()在某些情况下不匹配

时间:2016-11-04 06:19:19

标签: php preg-match

我在这里使用的代码适用于所有其他输入但不适用于hospital 为什么?

如果你建议任何其他过滤器,那么将非常感谢。

请参阅演示:http://phpio.net/s/cgv

这是我的代码:

$type = strtolower("hospital"); 

if(preg_match('/it|IT|it services|it service|it infrastructure|it infrastructures/',$type)){
    $img = "images/it1-200x111.jpg";
}else if(preg_match('/business|business solutions|biz solutions|business solution|biz solution/',$type)){
    $img = "images/business-200x111.jpg";
}else if(preg_match('/manpower|manpower solution|manpower solutions|manpower services|manpower service|human resource|human resources/',$type)){
    $img = "images/mp-1-200x111.jpg";
}else if(preg_match('/financial|finance|financial solution|financial solutions|financial service|financial services/',$type)){
    $img = "images/fin-200x111.jpg";
}else if(preg_match('/marketing|marketing solution|marketing solutions|marketing service|marketing services/',$type)){
    $img = "images/mark1-200x111.jpg";
}else if(preg_match('/real estate|real estates|real estate solution|real estates solution|real estate solutions/',$type)){
    $img = "images/real1-200x111.jpg";
}else if(preg_match('/school|schooling services|school services|schools service|schooling service/',$type)){
    $img = "images/school1-200x111.jpg";
}else if(preg_match('/college|college service|college services|colleges/',$type)){
    $img ="images/coll1-200x111.jpg";
}else if(preg_match('/hospital|hospitals|hospital service|hospital services/',$type)){
    $img = "images/hosp-1-200x111.jpg";
}else{
    $img = "images/it1-200x111.jpg";
}

echo $img;

1 个答案:

答案 0 :(得分:3)

hospital符合您的第一个条件,因为那里有it。 尝试在条件上添加开始/开始符号^。例如^it,它只会在字符串以it开头时匹配。