Skip to content

Instantly share code, notes, and snippets.

@marcus-at-localhost
Created April 13, 2020 18:27
Show Gist options
  • Save marcus-at-localhost/5c93f06ddb32e35400cbd2fc4a0f5f51 to your computer and use it in GitHub Desktop.
Save marcus-at-localhost/5c93f06ddb32e35400cbd2fc4a0f5f51 to your computer and use it in GitHub Desktop.
[Split by Comma but not if in Parenthesis] #regex #split #array
<?php
$filters = "1,2,4,(5,6,7),8;9";
var_dump(preg_split("~[,|;](?![^(]*\\))~", $filters));
/*
array (size=6)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '4' (length=1)
3 => string '(5,6,7)' (length=7)
4 => string '8' (length=1)
5 => string '9' (length=1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment