<i id='4zNAB'><tr id='4zNAB'><dt id='4zNAB'><q id='4zNAB'><span id='4zNAB'><b id='4zNAB'><form id='4zNAB'><ins id='4zNAB'></ins><ul id='4zNAB'></ul><sub id='4zNAB'></sub></form><legend id='4zNAB'></legend><bdo id='4zNAB'><pre id='4zNAB'><center id='4zNAB'></center></pre></bdo></b><th id='4zNAB'></th></span></q></dt></tr></i><div id='4zNAB'><tfoot id='4zNAB'></tfoot><dl id='4zNAB'><fieldset id='4zNAB'></fieldset></dl></div>
    • <bdo id='4zNAB'></bdo><ul id='4zNAB'></ul>

  1. <tfoot id='4zNAB'></tfoot>

    <legend id='4zNAB'><style id='4zNAB'><dir id='4zNAB'><q id='4zNAB'></q></dir></style></legend>

      <small id='4zNAB'></small><noframes id='4zNAB'>

      PHP:在逗号上拆分字符串,但不在大括号或引号之

      时间:2023-10-03
      1. <i id='7xbs4'><tr id='7xbs4'><dt id='7xbs4'><q id='7xbs4'><span id='7xbs4'><b id='7xbs4'><form id='7xbs4'><ins id='7xbs4'></ins><ul id='7xbs4'></ul><sub id='7xbs4'></sub></form><legend id='7xbs4'></legend><bdo id='7xbs4'><pre id='7xbs4'><center id='7xbs4'></center></pre></bdo></b><th id='7xbs4'></th></span></q></dt></tr></i><div id='7xbs4'><tfoot id='7xbs4'></tfoot><dl id='7xbs4'><fieldset id='7xbs4'></fieldset></dl></div>

        <tfoot id='7xbs4'></tfoot>

              <small id='7xbs4'></small><noframes id='7xbs4'>

              • <bdo id='7xbs4'></bdo><ul id='7xbs4'></ul>
                  <tbody id='7xbs4'></tbody>
                <legend id='7xbs4'><style id='7xbs4'><dir id='7xbs4'><q id='7xbs4'></q></dir></style></legend>
              • 本文介绍了PHP:在逗号上拆分字符串,但不在大括号或引号之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在 PHP 中,我有以下字符串:

                In PHP I have the following string :

                $str = "AAA, BBB, (CCC,DDD), 'EEE', 'FFF,GGG', ('HHH','III'), (('JJJ','KKK'), LLL, (MMM,NNN)) , OOO"; 
                

                我需要将此字符串拆分为以下部分:

                I need to split this string into the following parts:

                AAA
                BBB
                (CCC,DDD)
                'EEE'
                'FFF,GGG'
                ('HHH','III')
                (('JJJ','KKK'),LLL, (MMM,NNN))
                OOO
                

                我尝试了几个正则表达式,但找不到解决方案.有什么想法吗?

                I tried several regexes, but couldn't find a solution. Any ideas?

                更新

                在处理格式错误的数据、转义引号等时,我认为使用正则表达式并不是最好的解决方案.

                I've decided using regex is not really the best solution, when dealing with malformed data, escaped quotes, etc.

                感谢这里提出的建议,我找到了一个使用解析的函数,我重新编写了它以满足我的需要.它可以处理不同类型的括号,分隔符和引号也是参数.

                Thanks to suggestions made on here, I found a function that uses parsing, which I rewrote to suit my needs. It can handle different kind of brackets and the separator and quote are parameters as well.

                 function explode_brackets($str, $separator=",", $leftbracket="(", $rightbracket=")", $quote="'", $ignore_escaped_quotes=true ) {
                
                    $buffer = '';
                    $stack = array();
                    $depth = 0;
                    $betweenquotes = false;
                    $len = strlen($str);
                    for ($i=0; $i<$len; $i++) {
                      $previouschar = $char;
                      $char = $str[$i];
                      switch ($char) {
                        case $separator:
                          if (!$betweenquotes) {
                            if (!$depth) {
                              if ($buffer !== '') {
                                $stack[] = $buffer;
                                $buffer = '';
                              }
                              continue 2;
                            }
                          }
                          break;
                        case $quote:
                          if ($ignore_escaped_quotes) {
                            if ($previouschar!="\") {
                              $betweenquotes = !$betweenquotes;
                            }
                          } else {
                            $betweenquotes = !$betweenquotes;
                          }
                          break;
                        case $leftbracket:
                          if (!$betweenquotes) {
                            $depth++;
                          }
                          break;
                        case $rightbracket:
                          if (!$betweenquotes) {
                            if ($depth) {
                              $depth--;
                            } else {
                              $stack[] = $buffer.$char;
                              $buffer = '';
                              continue 2;
                            }
                          }
                          break;
                        }
                        $buffer .= $char;
                    }
                    if ($buffer !== '') {
                      $stack[] = $buffer;
                    }
                
                    return $stack;
                  }
                

                推荐答案

                代替 preg_split,做一个 preg_match_all:

                $str = "AAA, BBB, (CCC,DDD), 'EEE', 'FFF,GGG', ('HHH','III'), (('JJJ','KKK'), LLL, (MMM,NNN)) , OOO"; 
                
                preg_match_all("/((?:[^()]|(?R))+)|'[^']*'|[^(),s]+/", $str, $matches);
                
                print_r($matches);
                

                将打印:

                Array
                (
                    [0] => Array
                        (
                            [0] => AAA
                            [1] => BBB
                            [2] => (CCC,DDD)
                            [3] => 'EEE'
                            [4] => 'FFF,GGG'
                            [5] => ('HHH','III')
                            [6] => (('JJJ','KKK'), LLL, (MMM,NNN))
                            [7] => OOO
                        )
                
                )

                正则表达式 ((?:[^()]|(?R))+)|'[^']*'|[^(),s]+ 可以分为三部分:

                The regex ((?:[^()]|(?R))+)|'[^']*'|[^(),s]+ can be divided in three parts:

                1. ((?:[^()]|(?R))+),匹配括号的平衡对
                2. '[^']*' 匹配一个带引号的字符串
                3. [^(),s]+ 匹配任何不包含 '(', ')' 的字符序列,',' 或空白字符
                1. ((?:[^()]|(?R))+), which matches balanced pairs of parenthesis
                2. '[^']*' matching a quoted string
                3. [^(),s]+ which matches any char-sequence not consisting of '(', ')', ',' or white-space chars

                这篇关于PHP:在逗号上拆分字符串,但不在大括号或引号之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将文本拆分为单个单词 下一篇:使用 PHP 拆分大文件

                相关文章

                最新文章

              • <small id='bJzQZ'></small><noframes id='bJzQZ'>

                    <bdo id='bJzQZ'></bdo><ul id='bJzQZ'></ul>

                  <tfoot id='bJzQZ'></tfoot>
                1. <i id='bJzQZ'><tr id='bJzQZ'><dt id='bJzQZ'><q id='bJzQZ'><span id='bJzQZ'><b id='bJzQZ'><form id='bJzQZ'><ins id='bJzQZ'></ins><ul id='bJzQZ'></ul><sub id='bJzQZ'></sub></form><legend id='bJzQZ'></legend><bdo id='bJzQZ'><pre id='bJzQZ'><center id='bJzQZ'></center></pre></bdo></b><th id='bJzQZ'></th></span></q></dt></tr></i><div id='bJzQZ'><tfoot id='bJzQZ'></tfoot><dl id='bJzQZ'><fieldset id='bJzQZ'></fieldset></dl></div>
                  1. <legend id='bJzQZ'><style id='bJzQZ'><dir id='bJzQZ'><q id='bJzQZ'></q></dir></style></legend>