我想使用 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
)
)
最佳答案
这只是一个拼写错误(我认为),您已经包含了该角色!在第一个字符类中。删除它。
另外不要忘记,字符类只是一组没有顺序的字符,而不是一组字符串。换句话说,您不必编写类似 [()>=<={}=!()><!=!{}]
的内容。 , [><}{)(=!]
就足够了。
如果您不必检查操作的格式是否正确,您也可以使用格式化字符串而不是正则表达式以函数方式执行相同的操作:
$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
)
关于php - 无法在 php 中使用 preg_match 通过特殊符号分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429808/