2016-11-04 214 views
0

這裏我使用的代碼適用於所有其他輸入,但不適用於hospital爲什麼?爲什麼preg_match()在某些情況下不匹配

如果你建議任何其他過濾器,那麼這將是非常感激。

觀看演示: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; 
+0

你的意思是醫院將在網址..它不工作..? –

+1

,因爲''it' on''' – jamesjaya

+0

$ type確定要輸入的條件,但不輸入醫院區域,其他輸入 – EaBangalore

回答

3

hospital相匹配的第一個條件,因爲有it那裏。 嘗試在條件上添加開始/開始符號^。例如^it,它只會在字符串以it開頭時匹配。

+0

我可以在phpio.net上演示嗎 – EaBangalore

+0

http://phpio.net/s/ci0 – jamesjaya

相關問題