鉴于此字符串:
$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
)
答案 0 :(得分:4)
根据您给定的字符串,您可以使用以下内容...
$results = preg_split('/(?:<[^>]*>)?\K,/', $str);
print_r($results);
输出
Array
(
[0] => <[return date("Y-m-d", strtotime("yesterday"));]>
[1] => <[return "cool!";]>
[2] => TRUE
[3] => foo
)
当然,你可以匹配所有而不是使用拆分..
preg_match_all('/<[^>]+>|[^><,]+/', $str, $matches);
答案 1 :(得分:1)