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 6737011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:11:53+00:00 2026-05-26T11:11:53+00:00

Would like to turn text with nested parenthesis to a nested array. Here is

  • 0

Would like to turn text with nested parenthesis to a nested array. Here is an example output from an NLP parser:

(TOP (S (NP (PRP I)) (VP (VBP love) (NP (NP (DT a) (JJ big) (NN bed)) (PP (IN of) (NP (NNS roses))))) (. .)))

(orig: I love a big bed of roses.)

Would like to turn this into a nested array so it will look sg like this

TOP
 S
  NP
   PRP I
  VP 
   VBP Love

etc.

Found this php curly braces into array but that is not a nested array

  • 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-05-26T11:11:53+00:00Added an answer on May 26, 2026 at 11:11 am

    explanation by code:

    <?php
    
    class ParensParser
    {
        // something to keep track of parens nesting
        protected $stack = null;
        // current level
        protected $current = null;
    
        // input string to parse
        protected $string = null;
        // current character offset in string
        protected $position = null;
        // start of text-buffer
        protected $buffer_start = null;
    
        public function parse($string)
        {
            if (!$string) {
                // no string, no data
                return array();
            }
    
            if ($string[0] == '(') {
                // killer outer parens, as they're unnecessary
                $string = substr($string, 1, -1);
            }
    
            $this->current = array();
            $this->stack = array();
    
            $this->string = $string;
            $this->length = strlen($this->string);
            // look at each character
            for ($this->position=0; $this->position < $this->length; $this->position++) {
                switch ($this->string[$this->position]) {
                    case '(':
                        $this->push();
                        // push current scope to the stack an begin a new scope
                        array_push($this->stack, $this->current);
                        $this->current = array();
                        break;
    
                    case ')':
                        $this->push();
                        // save current scope
                        $t = $this->current;
                        // get the last scope from stack
                        $this->current = array_pop($this->stack);
                        // add just saved scope to current scope
                        $this->current[] = $t;
                        break;
                   /* 
                    case ' ':
                        // make each word its own token
                        $this->push();
                        break;
                    */
                    default:
                        // remember the offset to do a string capture later
                        // could've also done $buffer .= $string[$position]
                        // but that would just be wasting resources…
                        if ($this->buffer_start === null) {
                            $this->buffer_start = $this->position;
                        }
                }
            }
    
            return $this->current;
        }
    
        protected function push()
        {
            if ($this->buffer_start !== null) {
                // extract string from buffer start to current position
                $buffer = substr($this->string, $this->buffer_start, $this->position - $this->buffer_start);
                // clean buffer
                $this->buffer_start = null;
                // throw token into current scope
                $this->current[] = $buffer;
            }
        }
    }
    
    $string = '(TOP (S (NP (PRP I)) (VP (VBP love) (NP (NP (DT a) (JJ big) (NN bed)) (PP (IN of) (NP (NNS roses))))) (. .)))';
    $p = new ParensParser();
    $result = $p->parse($string);
    var_dump($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to get html-formatted data from mysql, and turn
I would like to duplicate Wikipedia's footnote highlighting from in-text citation click solely in
I would like to turn the HTML generated by my CFM page into a
I would like to turn this query to regular inline sql without using stored
I am getting expected notices and warnings and would like to turn them off
I'm using the Django ORM in a non-Django application and would like to turn
I have a grid view with a nested text box in it. I would
I have a plain text list like this: I am the first top-level list
String text = '[[item1,item2,item3], [some, item], [far, out, string]]'; I would like to iterate
I would like to find text which is between the < and > characters,

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.