2012-11-01 31 views
4
$date could be "23/09/2012" or "23-09-2012" or "23\09\2012" 
preg_split('/[\/\-\\]/', $date); 

不确定为什么PHP继续抛出missing terminating ] error编译失败:缺失终止]字符分类

+0

你需要逃避连字符? –

+0

@Xin旧的正则表达式中有一个错误。请参阅最新的答案。对于那个很抱歉。 –

回答

8
preg_split('/[\/\-\\]/', $date); 
        ^escaping the closing ']' 

请不要使用以下,以去除不确定性

preg_split('/[\/\-\\\\]/', $date); 

没有必要逃避-,但你可以使用\-为好。


代码:

$date = 'as\sad-s/p'; 
$slices = preg_split('/[\/\-\\\\]/', $date); 
print_r($slices); 

输出:

Array ([0] => as [1] => sad [2] => s [3] => p) 
+0

@Xin Chen:我的正则表达式有一个错误,因为它不是在'\\'分裂。请参阅最新的答案。 –