I need to manage escaping in a comma split. This is a string example:
我需要设法用逗号分隔符进行转义。这是一个字符串示例:
var1,t3st,ax_1,c5\,3,last
I need this split:
我需要这个分裂:
var1
t3st
ax_1
c5\,3
last
Please mind this: "c5\,3" is not splitted.
请注意:“c5\ 3”没有分裂。
I tried with this:
我试着用这个:
$expl=preg_split('#[^\\],#', $text);
But i loose the last char of each split.
但我松开了每个裂口的最后一个字符。
4 个解决方案
#1
2
use this regex
使用这个正则表达式
$str = 'var1,t3st,ax_1,c5\,3,last';
$expl=preg_split('#(?<!\\\),#', $str);
print_r($expl); // output Array ( [0] => var1 [1] => t3st [2] => ax_1 [3] => c5\,3 [4] => last )
working example http://codepad.viper-7.com/pWSu3S
http://codepad.viper - 7. com/pwsu3s工作示例
#2
1
Try with a lookbehind:
试着向后插入:
preg_split('#(?<!\\),#', $text);
#3
0
Do a 3 phase approach
采用三相法
First replace \, with someting "unique" like \\
首先,用一些“独特”的像\\。
Do your split by ","
用","
Replace \\ with \,
取代\ \ \,
That not as nice as regex but it will work ;)
虽然没有regex那么好,但它会起作用;)
#4
0
is this ok ?
这是好的吗?
<?php
$text = "var1,t3st,ax_1,c5\,3,last";
$text = str_replace("\,", "#", $text);
$xpode = explode(",", $text);
$new_text = str_replace("#", "\,", $xpode);
print_r($new_text);
?>
Output
输出
Array ( [0] => var1 [1] => t3st [2] => ax_1 [3] => c5\,3 [4] => last )
#1
2
use this regex
使用这个正则表达式
$str = 'var1,t3st,ax_1,c5\,3,last';
$expl=preg_split('#(?<!\\\),#', $str);
print_r($expl); // output Array ( [0] => var1 [1] => t3st [2] => ax_1 [3] => c5\,3 [4] => last )
working example http://codepad.viper-7.com/pWSu3S
http://codepad.viper - 7. com/pwsu3s工作示例
#2
1
Try with a lookbehind:
试着向后插入:
preg_split('#(?<!\\),#', $text);
#3
0
Do a 3 phase approach
采用三相法
First replace \, with someting "unique" like \\
首先,用一些“独特”的像\\。
Do your split by ","
用","
Replace \\ with \,
取代\ \ \,
That not as nice as regex but it will work ;)
虽然没有regex那么好,但它会起作用;)
#4
0
is this ok ?
这是好的吗?
<?php
$text = "var1,t3st,ax_1,c5\,3,last";
$text = str_replace("\,", "#", $text);
$xpode = explode(",", $text);
$new_text = str_replace("#", "\,", $xpode);
print_r($new_text);
?>
Output
输出
Array ( [0] => var1 [1] => t3st [2] => ax_1 [3] => c5\,3 [4] => last )