The string $s = ' A, B, C (x, y ("Z", 2, 0)), D, E ';
Split into arrays, the difficulty is C (x, y ("Z", 2, 0)), is a whole.
Desired Result:
Array
' A ',
' B ',
' C (x, y ("Z", 2, 0)) ',
' D ',
' E ');
Reply to discussion (solution)
$s = ' A, B, C (x, y ("Z", 2, 0)), D, E '; $keywords = Preg_split ("/\,\s (? =[a-z])/", $s); Var_dump ($keywords);
$s = ' A, B, C (x, y ("Z", 2, 0)), D, E '; $keywords = Preg_split ("/\,\s (? =[a-z])/", $s); Var_dump ($keywords);
-----------------------------
Thank you very much for your reply! But relying on the =[a-z] is inaccurate, because it is possible that the parentheses are also uppercase (within the nested parentheses, whatever the content is a whole).
$s = ' A, B, C (X, Y ("Z", 2, 0)), D, E ';
$s = ' A, B, C (x, y ("Z", 1, 0)), D (x, y ("Z", 2, 0)), e,f (x, y ("Z", 3, 0)) ';//extract Get inside Contents Preg_match_all ("/\ (. *?\) \)/", $s, $match) ;//This part can be modified by itself $s = str_replace ($match [0], '% ', $s);//will be replaced as a whole,% percent sign can also be selected $exs = Explode (', ', $s); $i = 0;foreach ($exs as $key = = $value) {if (Strpos ($value, "%")!== false) {$exs [$key] = str_replace ('% ', $match [0][$i], $value); $i + +;}} Var_dump ($EXS);
To help you complicate it.
So this is not the right thing to do, and honestly write a function than the struggle to write a regular more affordable
$s = ' A, B, C (x, Y ("Z", 2, 0)), D, E ';p rint_r (foo ($s)), function foo ($s) {$r = array (); $m = 0; $t = "; for ($i =0; $i
array ([0] = A [1] = B [2] = C (x, Y ("Z", 2, 0)) [3] = > D [4] + E)
thanks to 2-bit reply.
think of a matching parenthesis nested regular, with similar calculations can refer to. (no rigorous testing)
$s = ' A, B,c (X, Y ("Ez", 2, 0, Z (KK, a))), D, E (U (8, D (88))) ';
Preg_match_all ('/[a-z] (? =[,]) |[ ^,]*\(([^()]+| (? R)) *\)/', $s, $z);
Print_r ($z [0]);