Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php regexp按逗号和空格分割字符串,但忽略内部引号和括号_Php_Regex - Fatal编程技术网

Php regexp按逗号和空格分割字符串,但忽略内部引号和括号

Php regexp按逗号和空格分割字符串,但忽略内部引号和括号,php,regex,Php,Regex,我需要用逗号和空格分割字符串,但忽略内引号、单引号和括号 $str = "Questions, \"Quote\",'single quote','comma,inside' (inside parentheses) space #specialchar"; 因此,生成的数组将具有 [0]Questions [1]Quote [2]single quote [3]comma,inside [4]inside parentheses [5]space [6]#specialchar 但这忽略了特

我需要用逗号和空格分割字符串,但忽略内引号、单引号和括号

$str = "Questions, \"Quote\",'single quote','comma,inside' (inside parentheses) space #specialchar";
因此,生成的数组将具有

[0]Questions [1]Quote [2]single quote [3]comma,inside [4]inside parentheses [5]space [6]#specialchar 但这忽略了特殊字符,仍然在引号内拆分逗号,结果数组是:

[0]Questions [1]Quote [2]single quote [3]comma [4]inside [5]inside parentheses [6]space [7]specialchar [0]问题 [1] 引述 [2] 单引号 [3] 逗号 [4] 里面 [5] 内圆括号 [6] 空间 [7] 特种炭 ps:这不是csv


非常感谢

这只适用于非嵌套括号:

    $regex = <<<HERE
    /  "  ( (?:[^"\\\\]++|\\\\.)*+ ) \"
     | '  ( (?:[^'\\\\]++|\\\\.)*+ ) \'
     | \( ( [^)]*                  ) \)
     | [\s,]+
    /x
    HERE;

    $tags = preg_split($regex, $str, -1,
                         PREG_SPLIT_NO_EMPTY
                       | PREG_SPLIT_DELIM_CAPTURE);

$regex=好吧,这适用于您提供的数据:

$rgx = <<<'EOT'
/
  [,\s]++
  (?=(?:(?:[^"]*+"){2})*+[^"]*+$)
  (?=(?:(?:[^']*+'){2})*+[^']*+$)
  (?=(?:[^()]*+\([^()]*+\))*+[^()]*+$)
/x
EOT;

$rgx=inshallah。您知道在javascript split()函数中使用相同的regexp吗?如果你能告诉我就好了。@unknown,我想
/x
标志和
*+
+
量词可能不受支持,因此,请丢失
/x
标志并去掉所有空白(包括换行符),量词分别使用
*
+
而不是
*
+
$rgx = <<<'EOT'
/
  [,\s]++
  (?=(?:(?:[^"]*+"){2})*+[^"]*+$)
  (?=(?:(?:[^']*+'){2})*+[^']*+$)
  (?=(?:[^()]*+\([^()]*+\))*+[^()]*+$)
/x
EOT;
"not a \" quote", 'not a ) quote', (not ",' quotes)