Regular Expression PHP Help with if not equal to
I am trying to use regular expressions to make my inventory upload script a little better.
I sell clothing and accessories. In my inventory file I have a field called ProductDepartment which is a combination of gender/item type. I have a MySQL table for gender with the following values:
Mens = 开发者_StackOverflow中文版1 Womens = 2 Unisex = 3
Departments in the uploaded file look like the following, for example.
Mens Outerwear Mens Watches Womens Jeans Headphones
So basically Im trying to make a statment that goes, if $ProductDept contains Mens, set $gender_id = 1, if $ProductDept contains Womens, set $gender_id = 2, if $ProductDept does not contain either, set $gender_id = 3
And I'm having trouble figuring out how to write the 3rd if statement correctly. Here's what I have.
if(preg_match('/Mens/g', $ProductDept)) {
$gender_id = '1';
}
if(preg_match('/Womens/g', $ProductDept)) {
$gender_id = '2';
}
if( != preg_match('/Mens|Womens/g', $ProductDept)) {
$gender_id = '3';
}
Change !=
to !
:
if (!preg_match('/Mens|Womens/g', $ProductDept)) { ... }
You could also rewrite it using elseif as follows:
if (preg_match('/Womens/g', $ProductDept)) {
$gender_id = '2';
} elseif (preg_match('/Mens/g', $ProductDept)) {
$gender_id = '1';
} else {
$gender_id = '3';
}
I'll do :
if(preg_match('/Womens/', $ProductDept)) {
$gender_id = '2';
} elseif(preg_match('/Mens/', $ProductDept)) {
$gender_id = '1';
} else {
$gender_id = '3';
}
精彩评论