简体   繁体   中英

Regular expressions php split

I need to split a string like '7-2014' into an array [7],[2014] in PHP.

For this special case,

$arr = preg_split('/-/','7-2014'); 

works fine, but how can I make an expression that splits on any of these characters: . / \\ space . / \\ space ?

You can create a character class :

$arr = preg_split('%[-./\\\\ ]%', $subject);

This will split along dashes, dots, slashes, backslashes and spaces.

使用方括号定义多个字符:

$arr = preg_split('#[-\\.\\\\\/ ]#','7-2014');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM