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 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入