简体   繁体   English

正则表达式PHP帮助,如果不等于

[英]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. 在我的库存文件中,我有一个名为ProductDepartment的字段,它是性别/项目类型的组合。 I have a MySQL table for gender with the following values: 我有一个性别的MySQL表,其中包含以下值:

Mens = 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 所以基本上我试图做出一个判断,如果$ ProductDept包含Mens,设置$ gender_id = 1,如果$ ProductDept包含Womens,设置$ gender_id = 2,如果$ ProductDept不包含,请设置$ gender_id = 3

And I'm having trouble figuring out how to write the 3rd if statement correctly. 而且我无法弄清楚如何正确地编写第3条if语句。 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: 您也可以使用elseif重写它,如下所示:

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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM