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)