我想通过使用preg_match函数将某些特定符号拆分为字符串但不起作用。
这是我尝试的代码,但有点我无法解决问题。
所以问题出在输出数组中,你可以看到1,4和6索引没有通过!= , !{}
和!()
符号正确分割字符串,其余符号正确分割。请帮助我。
I am spliting string by using following symbols
=
!=
>=
<=
>
<
{}
!{}
()
!()
<?php
echo "<pre>";
$action_array = array(
'sku{}NP-POCH-B15-C12,NP-FGV-C15',
'brand!=nutra,avc',
'category_ids=1,4,5',
'quote_item_qty>=5',
'quote_item_row_total!{}140',
'attribute_set_id<=4',
'attribute_set_list!()4,5,7',
'quote_grand_total()100',
'grand_total>100',
'grand_total<100',
);
$action_format = array();
echo "<ul>\n";
echo "<pre>";
print_r($action_array);
foreach($action_array as $key => $statement) {
$reg_ex = '/([a-zA-Z_!]*)([()>=<={}=!()><!=!{}]*)([a-zA-Z0-9-,]*)/';
$matches = array();
$rslt = preg_match($reg_ex, $statement, $matches);
$action_format[] = array(
'type' => 'salesrule/rule_condition_product',
'attribute' => $matches[1],
'operator' => $matches[2],
'value' => $matches[3]
);
//echo "<li>(".$rslt.") [".$statement."] ".print_r($matches,true)."</li>\n";
}
echo "</ul>\n";
echo "<p>\$action_format: <pre>".print_r($action_format,true)."</pre></p>\n";
?>
输出:
Array
(
[0] => sku{}NP-POCH-B15-C12,NP-FGV-C15
[1] => brand!=nutra,avc
[2] => category_ids=1,4,5
[3] => quote_item_qty>=5
[4] => quote_item_row_total!{}140
[5] => attribute_set_id<=4
[6] => attribute_set_list!()4,5,7
[7] => quote_grand_total()100
[8] => grand_total>100
[9] => grand_total<100
)
$action_format:
Array
(
[0] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => sku
[operator] => {}
[value] => NP-POCH-B15-C12,NP-FGV-C15
)
[1] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => brand!
[operator] => =
[value] => nutra,avc
)
[2] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => category_ids
[operator] => =
[value] => 1,4,5
)
[3] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => quote_item_qty
[operator] => >=
[value] => 5
)
[4] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => quote_item_row_total!
[operator] => {}
[value] => 140
)
[5] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => attribute_set_id
[operator] => <=
[value] => 4
)
[6] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => attribute_set_list!
[operator] => ()
[value] => 4,5,7
)
[7] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => quote_grand_total
[operator] => ()
[value] => 100
)
[8] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => grand_total
[operator] => >
[value] => 100
)
[9] => Array
(
[type] => salesrule/rule_condition_product
[attribute] => grand_total
[operator] => <
[value] => 100
)
)
答案 0 :(得分:2)
这只是一个错字(我想),你已经包含了这个角色!在第一个字符类中。删除它。
另外请不要忘记,字符类只是一组没有顺序的字符,而不是一组字符串。换句话说,您不必编写[()>=<={}=!()><!=!{}]
,[><}{)(=!]
等内容。
如果您不必检查操作是否格式正确,您还可以使用格式化字符串而不是正则表达式以功能方式执行相同的操作:
$format = '%[^{}()=<>!]%[{}()=<>!]%s';
$proto = ['type' => 'salesrule/rule_condition_product'];
$keys = ['attribute', 'operator', 'value'];
$action_format = [];
foreach ($action_array as $action) {
$action_format[] = $proto + array_combine($keys, sscanf($action, $format));
}
(如果您希望功能更强大,请不要使用foreach
并使用array_map
)
答案 1 :(得分:1)
您可以使用此正则表达式模式,使用替代运算符|
来表示所有预期的字符串。必须转义字符()<>{}
才能使其正常工作。
$reg_ex = '/([a-zA-Z_]*)(\(\)|\>=|\<=|\{\}|=|!\(\)|\>|\<|!=|!\{\})([a-zA-Z0-9-,]*)/';