Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:14:48+00:00 2026-06-14T05:14:48+00:00

I have a string like first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth I want to explode it to array Array

  • 0

I have a string like

"first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth"

I want to explode it to array

Array (
    0 => "first",
    1 => "second[,b]",
    2 => "third[a,b[1,2,3]]",
    3 => "fourth[a[1,2]]",
    4 => "sixth"
}

I tried to remove brackets:

preg_replace("/[ ( (?>[^[]]+) | (?R) )* ]/xis", 
             "",
             "first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth"
); 

But got stuck one the next step

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:14:49+00:00Added an answer on June 14, 2026 at 5:14 am

    PHP’s regex flavor supports recursive patterns, so something like this would work:

    $text = "first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth";
    
    preg_match_all('/[^,\[\]]+(\[([^\[\]]|(?1))*])?/', $text, $matches);
    
    print_r($matches[0]);
    

    which will print:

    Array
    (
        [0] => first
        [1] => second[,b]
        [2] => third[a,b[1,2,3]]
        [3] => fourth[a[1,2]]
        [4] => sixth
    )

    The key here is not to split, but match.

    Whether you want to add such a cryptic regex to your code base, is up to you 🙂

    EDIT

    I just realized that my suggestion above will not match entries starting with [. To do that, do it like this:

    $text = "first,second[,b],third[a,b[1,2,3]],fourth[a[1,2]],sixth,[s,[,e,[,v,],e,],n]";
    
    preg_match_all("/
        (             # start match group 1
          [^,\[\]]    #   any char other than a comma or square bracket
          |           #   OR
          \[          #   an opening square bracket
          (           #   start match group 2
            [^\[\]]   #     any char other than a square bracket
            |         #     OR
            (?R)      #     recursively match the entire pattern
          )*          #   end match group 2, and repeat it zero or more times
          ]           #   an closing square bracket
        )+            # end match group 1, and repeat it once or more times
        /x", 
        $text, 
        $matches
    );
    
    print_r($matches[0]);
    

    which prints:

    Array
    (
        [0] => first
        [1] => second[,b]
        [2] => third[a,b[1,2,3]]
        [3] => fourth[a[1,2]]
        [4] => sixth
        [5] => [s,[,e,[,v,],e,],n]
    )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have a string of the form first;second;third;fourth I would like to print
For example I have a string like this: first:second:thirdtest:test:fourth I want to count the
I have a string like first url, second url, third url and would like
I have a string like this: |T1| This is some text for the first
I have the following String First Last <first.last@email.com> I would like to extract first.last
I'd like to capitalize the first letter of a string which could have special
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have string like {param1=foo}{param2=bar}hello world! I need to extract array of tuples (paramName,
I have a string like: hn$8m3kj4.23hs@8; i need to split it as follow: first
I have a string like: 1 this is my first text 2 his is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.