Compilation failed: missing terminating ] for char

2019-02-10 18:41发布

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

Not sure why PHP keep throw missing terminating ] error?

1条回答
相关推荐>>
2楼-- · 2019-02-10 19:25
preg_split('/[\/\-\\]/', $date);
                   ^escaping the closing ']' 

Do the following instead, to remove ambiguity

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

There is no need to escape -, but you could use \- as well.


Code:

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

Output:

Array ( [0] => as [1] => sad [2] => s [3] => p )
查看更多
登录 后发表回答