PHP Regex - comma delimited string, IGNORE comma between tags

With this line in mind:

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

      

How to get an array like this:

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

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

[2] => TRUE

[3] => foo
)

      

+3


source to share


2 answers


Based on this line, 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 everything instead of using split.

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

      

+4


source


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

      

Try it. Take a look at demo.Replace at \n

.



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

+1


source







All Articles