douchun5976 2017-04-15 18:35
浏览 80
已采纳

不能使用php中的preg_match通过特殊符号拆分字符串

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
        )

)
  • 写回答

2条回答 默认 最新

  • dongsuoying9059 2017-04-15 18:58
    关注

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

    demo

    (If you want to be more functional, don't use foreach and use array_map)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算