richard
richard

Reputation: 12498

PHP Regex - Split string with comma delimiter, IGNORE comma's between tags

Given this string:

$somethingawesome = '<[return date("Y-m-d", strtotime("yesterday"));]>,<[return "cool!";]>,TRUE,foo';

How do I get an array like this:

Array
(
[0] => <[return date("Y-m-d", strtotime("yesterday"));]>

[1] => <[return "cool!";]>

[2] => TRUE

[3] => foo
)

Upvotes: 3

Views: 684

Answers (2)

hwnd
hwnd

Reputation: 70722

Based off your given string, you can use the following ...

$results = preg_split('/(?:<[^>]*>)?\K,/', $str);
print_r($results);

Output

Array
(
    [0] => <[return date("Y-m-d", strtotime("yesterday"));]>
    [1] => <[return "cool!";]>
    [2] => TRUE
    [3] => foo
)

Or of course you could match all instead of using split ..

preg_match_all('/<[^>]+>|[^><,]+/', $str, $matches);

Upvotes: 4

vks
vks

Reputation: 67968

 ,(?=(?:[^\]\[]*\[[^\]]*\])*[^\]\[]*$)

Try this.See demo.Replace by \n.

http://regex101.com/r/aT7wM2/1

Upvotes: 1

Related Questions