对于字符类,编译失败:缺少终止]

时间:2012-11-01 09:07:09

标签: php preg-split

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

不确定为什么PHP会继续抛出missing terminating ] error

1 个答案:

答案 0 :(得分:11)

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 )
相关问题