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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:24:07+00:00 2026-05-26T14:24:07+00:00

I was wondering if it’s possible to do something like this. I’m having the

  • 0

I was wondering if it’s possible to do something like this.

I’m having the following string:

Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%|Child

And I’d like to explode it into something like:

-Parent1
---MiddleA
------Child
------Child
------Child
---MiddleB
------Child
------Child
------Child
-Parent2
---MiddleA
------Child

I don’t understand how to explode it and then explode it again to create an output like above.

The idea is that every parent will be identified with a trailing @, every child of that parent will have a trailing % and every child of that child will have a trailing|.

  • 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-26T14:24:08+00:00Added an answer on May 26, 2026 at 2:24 pm

    The data you give as input in your question:

    Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%|Child
    

    is not really well fitting to produce the output you ask for. It’s even syntactically incorrect at it’s end, the delimiters in MiddleA%|Child specifically.

    Correnting this, you can easily do it with preg_split:

    $str = 'Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%Child|';
    
    $elements = preg_split('~([^@%|]+?[@%|])~', $str, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    
    $types = '@%|';
    foreach($elements as $element)
    {
        $label = substr($element, 0, -1);
        $type = substr($element, -1);
        $indentValue = strpos($types, $type);
        if (FALSE === $indentValue) throw new Exception('Invalid string given.');
        $indent = str_repeat('-', $indentValue * 2 + 1);
        printf("%s %s\n", $indent, $label);
    }
    

    If you don’t have the input string in a valid format, you need to fix it first, e.g. with an appropriate parser or you need to react on the bogus cases inside the foreach loop.

    Edit: This is an modified example that turns the string into a tree-like structure so you can output it with nested foreach loops:

    $str = 'Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%Child|';
    
    $types = '@%|';
    $pattern = sprintf('~([^%1$s]+?[%1$s])~', $types);
    $elements = preg_split($pattern, $str, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    
    $tree = array();
    foreach($elements as $element)
    {
        $label = substr($element, 0, -1);
        $type = substr($element, -1);
        $indentValue = strpos($types, $type);
        if (FALSE === $indentValue) throw new Exception('Invalid string given.');
    
        $add =& $tree;
        for($i = $indentValue; $i--;)
        {
            $index = max(0, count($add)-1);
            $add =& $add[$index][1];
        }
        $add[] = array($label, array());
        unset($add);
    }
    
    foreach($tree as $level1)
    {
        echo '<div>', $level1[0], "\n";
        foreach($level1[1] as $level2)
        {
            echo '  <h3>', $level2[0], '</h3>', "\n";
            foreach($level2[1] as $level3)
            {
                echo '    <span>', $level3[0],'</span>', "\n";
            }
        }
        echo '</div>', "\n";
    }
    

    (Demo) – Hope this is helpful.

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

Sidebar

Related Questions

wondering how I can replace all special chars on my string like: hello this
Wondering if this is possible or something with this effect. public class MyModel {
Wondering if this is possible. We have an 3rd Party library that contains identification
Wondering if anyone out there has ran into this before.... I'd like to use
Wondering if I need to do something in my swf to be able to
Wondering if anyone can help me with this annoying but trivial (in terms of
Wondering if anyone has a good solution for this. My app is displaying nothing
Wondering how does an application like Process Explorer or Combo Fix detect hidden process
Wondering what would be the most efficient way to do the following. Game play.
Wondering if it's possible to change a value returned from a queryset before sending

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.