Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation failed: missing terminating ] for character class

Tags:

php

preg-split

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

like image 817
Xin Chen Avatar asked Nov 01 '12 09:11

Xin Chen


Video Answer


1 Answers

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 )
like image 156
Anirudh Ramanathan Avatar answered Oct 12 '22 20:10

Anirudh Ramanathan