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

  • SEARCH
  • Home
  • 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 9241059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:18:01+00:00 2026-06-18T08:18:01+00:00

I would like to know how I could transform the given string into the

  • 0

I would like to know how I could transform the given string into the specified array:

String

all ("hi there \(option\)", (this, that), other) another

Result wanted (Array)

[0] => all,
[1] => Array(
    [0] => "hi there \(option\)",
    [1] => Array(
        [0] => this,
        [1] => that
    ),
    [2] => other
),
[2] => another

This is used for a kind of console that I’m making on PHP.
I tried to use preg_match_all but, I don’t know how I could find parentheses inside parentheses in order to “make arrays inside arrays”.

EDIT

All other characters that are not specified on the example should be treated as String.

EDIT 2

I forgot to mention that all parameter’s outside the parentheses should be detected by the space character.

  • 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-18T08:18:02+00:00Added an answer on June 18, 2026 at 8:18 am

    There’s no question that you should write parser if you are building syntax tree. But if you just need to parse this sample input regex still might be a tool:

    <?php
    $str = 'all, ("hi there", (these, that) , other), another';
    
    $str = preg_replace('/\, /', ',', $str); //get rid off extra spaces
    /*
     * get rid off undefined constants with surrounding them with quotes
    */
    $str = preg_replace('/(\w+),/', '\'$1\',', $str);
    $str = preg_replace('/(\w+)\)/', '\'$1\')', $str);
    $str = preg_replace('/,(\w+)/', ',\'$1\'', $str);
    
    $str = str_replace('(', 'array(', $str);
    
    $str = 'array('.$str.');';
    
    echo '<pre>';
    eval('$res = '.$str); //eval is evil.
    print_r($res); //print the result
    

    Demo.

    Note: If input will be malformed regex will definitely fail. I am writing this solution just in a case you need fast script. Writing lexer and parser is time-consuming work, that will need lots of research.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there is any way that I could build
I would like to split a string along whitespaces, and I know that the
I would like to know how could I use JSON for passing data from
I'm just learning Erlang with Chicago Boss and would like to know how could
I would like to know whether I could perform a full outer join between
I would like to know if it could be possible to add a file
I would like to know how I could edit mp3 files on the server
I would like to know if phones with Android 2.3 or lower could download
I would like to know if an instance implements a specific method. I could
Do you know any way that I could programmatically or via scrirpt transform a

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.