EaBangalore EaBangalore - 1 year ago 56
PHP Question

why preg_match() does not match in some cases

here i'm using a code which works for all other input but not for


if you suggest any other filter then this it will be much appriciated.

see the demo: http://phpio.net/s/cgv

here is my code

$type = strtolower("hospital"); // change type for other services

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";


$img = "images/it1-200x111.jpg";
//die('there are no such service currently available');
} // end of if statement

echo $img;

see the demo: http://phpio.net/s/cgv

Answer Source

hospital matches your first condition because there is it there. Try add start/beginning symbol ^ on the conditions. For example ^it, it will only match when string starts with it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download