给定此字符串:
$somethingawesome = '<[return date("Y-m-d", strtotime("yesterday"));]>,<[return "cool!";]>,TRUE,foo';
如何获得这样的数组:
Array
(
[0] => <[return date("Y-m-d", strtotime("yesterday"));]>
[1] => <[return "cool!";]>
[2] => TRUE
[3] => foo
)
基于给定的字符串,您可以使用以下。。。
$results = preg_split('/(?:<[^>]*>)?'K,/', $str);
print_r($results);
输出
Array
(
[0] => <[return date("Y-m-d", strtotime("yesterday"));]>
[1] => <[return "cool!";]>
[2] => TRUE
[3] => foo
)
或者你当然可以匹配所有而不是使用split。。
preg_match_all('/<[^>]+>|[^><,]+/', $str, $matches);
,(?=(?:[^']'[]*'[[^']]*'])*[^']'[]*$)
试试这个。请参阅演示。替换为'n
。
http://regex101.com/r/aT7wM2/1