I want to split string by some specific symbols by using preg_match function but not working .
Here is my code which I tried but little bit I can not solve a problem.
So Problem is in output array you can see that 1 ,4 and 6 index not spliting string properly by != , !{}
and !()
symbols and rest of symbols properly split.SO HELP ME.
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>
";
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>
";
}
echo "</ul>
";
echo "<p>\$action_format: <pre>".print_r($action_format,true)."</pre></p>
";
?>
OUTPUT :
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
)
)
It's only a typo (I think), you have included the character ! in the first character class. Remove it.
Also don't forget that a character class is only a set of characters without order, not a set of strings. In other words you don't have to write something like [()>=<={}=!()><!=!{}]
, [><}{)(=!]
suffices.
If you don't have to check if actions are well formatted, you can also do the same thing in a functional way, using a formatted string instead of a regex:
$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));
}
(If you want to be more functional, don't use foreach
and use array_map
)
You can use this regex pattern utilizing an alternative operator |
for all expected strings. Characters ()<>{}
have to be escaped to make it work.
$reg_ex = '/([a-zA-Z_]*)(\(\)|\>=|\<=|\{\}|=|!\(\)|\>|\<|!=|!\{\})([a-zA-Z0-9-,]*)/';