我想从表单中过滤用户输入,所有变量都设置正确。正则表达式仅在最后一个 if 上失败,我在其中比较其他产品属性、尺寸、重量或尺寸。只有尺寸参数被正确过滤,其他参数被忽略,因为我可以把任何东西放在重量和尺寸上,正则表达式会接受它。例如:
Test input:
Size: aaaa // Does not let through
Weight: aaaa // Lets through
Dimensions: aaaa // Lets through
最后一个if的代码中有OR,因为只有一个参数是必须提交的。
这是负责正则表达式的代码:
if(!empty($UI->sku) && !empty($UI->name) && !empty($UI->price)) {
if(!empty($UI->size) || !empty($UI->weight) || !empty($UI->dimensions)) {
if(preg_match("/^\d+$/" , $UI->sku)) { // Checking if sku is int
if(preg_match("/^[a-zA-Z0-9 ]+$/" , $UI->name)) { // Checking if name consists only of numbers and letters
if(preg_match("/^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/" , $UI->price)) { // Checking if price is float or int
if(preg_match("/^[0-9]*$/" , $UI->size) || preg_match("/^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/" , $UI->weight) || preg_match("/^[0-9]+x[0-9]+x[0-9]+$/" , $UI->dimensions)) {
//Checking additional product parameters for valid format
$UI->addToDatabase();
} else {
echo "Check your product's additional property!";
exit;
}
} else {
echo "Check your product's price!";
exit;
}
} else {
echo "Check your product's name!";
exit;
}
} else {
echo "Check your product's SKU!";
exit;
}
} else {
echo "Please add additional parameter to your product!";
exit;
}
} else{
echo "Please, include all the product's parameters!";
}