【问题标题】:Convert string with two delimiters into flat associative array将带有两个分隔符的字符串转换为平面关联数组
【发布时间】:2017-06-14 05:17:54
【问题描述】:

我真的不知道正则表达式... 所以我被卡住了......谁能给我一个解释正则表达式本身的解决方案?

这是我的代码:

$str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935";
$pregsplit = preg_split("/[\s|]+/",$string2);

输出:

Array
(
    [0] => id:521082299088
    [1] => name:JOHNSON
    [2] => GREIG
    [3] => DENOIA
    [4] => mounth:JAN17
    [5] => amount:170027
    [6] => admin:2500
    [7] => billqty:1
    [8] => metre:R1/900
    [9] => usage:00010261-00010550
    [10] => reffno:0BKP21851AF3EC2E0D4F56997EA19DFA
    [11] => charge:170377
    [12] => balance:1935
)

我想要这样的输出:

Array
(
    "id" => 521082299088
    "name" => "JOHNSON GREIG DENOIA"
    "mount" => "JAN17"
    "amount" => 170027
    "admin" => 2500
    "billqty" => 1
    "metre" => "R1/900"
    "usage" => "00010261-00010550"
    "reffno" => "0BKP21851AF3EC2E0D4F56997EA19DFA"
    "charge" => 170377
    "balance" => 1935
)

【问题讨论】:

    标签: php regex explode


    【解决方案1】:

    1) 使用带有特定正则表达式模式的preg_match_all 函数的解决方案:

    $str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935";
    
    preg_match_all("/(\w+):([^|]+)/", $str, $matches, PREG_SET_ORDER);
    $result = [];
    foreach ($matches as $items) {
        $result[$items[1]] = $items[2];
    }
    // $items[1] contains a "parameter" name captured by the first capturing group (\w+)
    // $items[2] contains a "parameter" value captured by the second capturing group ([^|]+)
    
    print_r($result);
    

    输出:

    Array
    (
        [id] => 521082299088
        [name] => JOHNSON GREIG DENOIA
        [mounth] => JAN17
        [amount] => 170027
        [admin] => 2500
        [billqty] => 1
        [metre] => R1/900
        [usage] => 00010261-00010550
        [reffno] => 0BKP21851AF3EC2E0D4F56997EA19DFA
        [charge] => 170377
        [balace] => 1935
    )
    

    (\w+) - 匹配后跟:的所有字母数字字符

    ([^|]+) - 匹配除分隔符| 之外的所有字符

    http://php.net/manual/en/function.preg-match-all.php


    2) 除了第一种方法 - 使用 array_combine 函数(组合来自两个捕获组的所有相应值):

    preg_match_all("/(\w+):([^|]+)/", $str, $matches);
    $result = array_combine($matches[1], $matches[2]);
    // will give the same result
    

    3) 第三种替代方法是使用explode() 函数:

    $result = [];
    foreach (explode("|", $str) as $items) {
        $pair = explode(":", $items);
        $result[$pair[0]] = $pair[1];
    }
    

    【讨论】:

    • 你能给我解释一下那个正则表达式吗?每个字符
    • @KurniawanNanda,添加解释
    • 您的表达中似乎不需要积极的前瞻。
    • @KurniawanNanda 我建议你搜索在线实时正则表达式编辑工具来练习它。这些将有助于更好地理解正则表达式。例如,我真的会推荐 regexr.com。
    • @Niitaku,好的,已删除
    【解决方案2】:

    这种方法是一种替代方法。

    您可以使用 PHP 的 explode() 函数分离字符串并从中创建一个数组。然后您可以使用strpos()substr() 函数分离'key:value' 结构。

    // input string
    $str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935";
    
    // make an array out of the string, split elements on each pipe character ('|')
    $arr = explode('|', $str);
    
    // create an output array to keep the results
    $output = [];
    
    // process the array
    foreach ($arr as $item) {
        // get delimiter
        $separatorPos = strpos($item, ':');
        // take the key part (The part before the ':')
        $key = substr($item, 0, $separatorPos);
        // take the value part (The part after the ':')
        $value = substr($item, $separatorPos);
        // push it into the output array
        $output[$key] = $value;
    }
    
    // dump the output array
    var_export($output);
    

    输出数组的转储如下所示;

    [
        'id'      => ':521082299088',
        'name'    => ':JOHNSON GREIG DENOIA',
        'mounth'  => ':JAN17',
        'amount'  => ':170027',
        'admin'   => ':2500',
        'billqty' => ':1',
        'metre'   => ':R1/900',
        'usage'   => ':00010261-00010550',
        'reffno'  => ':0BKP21851AF3EC2E0D4F56997EA19DFA',
        'charge'  => ':170377',
        'balace'  => ':1935',
    ]
    

    【讨论】:

      【解决方案3】:

      如果您无法编写正则表达式。这里是使用explode() 方法的简单解决方案。explode() 函数将字符串分解为数组。

      <?php
      $str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935";
      
      $array = explode('|',$str);
      foreach($array as $key=>$value){
       $data = explode(':',$value);
       $final[$data[0]] = $data[1];
      
      }
      print_r($final);
      
      ?>
      

      输出:

      Array
      (
          [id] => 521082299088
          [name] => JOHNSON GREIG DENOIA
          [mounth] => JAN17
          [amount] => 170027
          [admin] => 2500
          [billqty] => 1
          [metre] => R1/900
          [usage] => 00010261-00010550
          [reffno] => 0BKP21851AF3EC2E0D4F56997EA19DFA
          [charge] => 170377
          [balace] => 1935
      )
      

      要了解有关explode() 的更多信息,请阅读文档http://php.net/manual/en/function.explode.php

      【讨论】:

        【解决方案4】:

        一种有趣的方式(仅当您的字符串不包含=&amp; 时):将管道转换为和号,将冒号转换为等号,然后将其解析为带有parse_str 的URL 查询:

        $str = "id:521082299088|name:JOHNSON GREIG DENOIA|mounth:JAN17|amount:170027|admin:2500|billqty:1|metre:R1/900|usage:00010261-00010550|reffno:0BKP21851AF3EC2E0D4F56997EA19DFA|charge:170377|balace:1935";
        
        parse_str(strtr($str, ':|', '=&'), $result);
        
        print_r($result);
        

        demo

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-14
          • 2011-08-31
          • 2019-08-23
          • 1970-01-01
          • 2022-12-31
          • 2023-01-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多