Compilation failed: missing terminating ] for character class

放肆的年华 提交于 2019-12-18 17:14:25

问题


$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:


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 )


来源:https://stackoverflow.com/questions/13173890/compilation-failed-missing-terminating-for-character-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!