Split into arrays, the difficulty is C (x, y ("Z", 2, 0)), is a whole.

Source: Internet
Author: User
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]);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.