编译失败:缺少字符类的终止符 [英] Compilation failed: missing terminating ] for character class

查看:94
本文介绍了编译失败:缺少字符类的终止符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Not sure why PHP keep throw missing terminating ] error?

推荐答案

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.

代码:

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

输出:

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

这篇关于编译失败:缺少字符类的终止符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆