-->

编译失败:缺少终端]字符类(Compilation failed: missing terminat

2019-07-03 11:39发布

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

不知道为什么PHP保持掷missing terminating ] error

Answer 1:

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 )


文章来源: Compilation failed: missing terminating ] for character class