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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:40:52+00:00 2026-06-05T00:40:52+00:00

Let’s say we have a tag called test, i.e. [code] . What I’d like

  • 0

Let’s say we have a tag called test, i.e. [code].

What I’d like to do, is I’d like to only allow up to X other [code] tags inside of every main [code] tag in a string, which means the most inner ones would get removed.

So, for example, if X = 4, the following string:

[code]a[code]b[code]c[code]d[code]e[code]f[code]g[/code][/code][/code][/code][/code][/code][/code]

Would become:

[code]a[code]b[code]c[code]d[code]e[/code][/code][/code][/code][/code]

And the following string:

[code]a[code]b[code]c[code]d[code]TEST[/code][code]e[code]f[code]g[/code][/code][/code][/code][/code][/code][/code]

Would become:

[code]a[code]b[code]c[code]d[code]TEST[/code][code]e[/code][/code][/code][/code][/code]

The goal here is to not have more than a few nested [code] elements inside of a code element, so it doesn’t get too messy.

I’m wondering how to implement this, just trying to think of an algorithm and would appreciate any suggestions.

  • 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-05T00:40:53+00:00Added an answer on June 5, 2026 at 12:40 am

    This is gonna be quite a waste since it would be so easy to add multiple tag support and so on here.
    You pretty much have to do fully blown out tree parsing either way.

    Note that invalid input is not handled in any way, tags must be properly balanced

    function get_node_contents( $node ) {
        $orig = $node;
        $ret = "[code]" . $node->content;
    
        if( @$node->children ) {
            foreach( $node->children as $node ) {
                $ret .= get_node_contents( $node );
            }
        }
    
    
        if( @$orig->endContent ) {
            $ret .= $orig->endContent;
        }
        return $ret."[/code]";
    
    }
    
    function reduce_depth( $str, $maxDepth = 4 ) {
        $index = 0;
        $len = strlen( $str );
        $reg = '/(\[code\]|\[\/code\])/';
    
        $root = new stdClass;
        $root->children = array();
        $depth = 0;
        $ret = "";
    
        $pos = strpos( $str, "[code]" );
    
        if( $pos ) {
            $ret .= substr( $str, 0, $pos - 0);
        }
    
        while( $index < $len  ) {
    
            if( !preg_match( $reg, $str, $matches, PREG_OFFSET_CAPTURE, $index )) {
                break;
            }
    
            $index = ( $matches[1][1] + strlen( $matches[1][0] ) );
            $tag = $matches[1][0];
    
            $next = preg_match( $reg, $str, $matches, PREG_OFFSET_CAPTURE, $index );
            $content = "";
    
            if( $next ) {
                $content = substr( $str, $index, $matches[1][1] - $index );
            }
    
            if( $tag === "[code]" ) {
                if( $depth === 0 ) {
                    $parent = $root->children[] = new stdClass;
                    $parent->content = $content;
                    $depth++;
                }
                else if ( $depth++ > $maxDepth ) {
    
                    continue;
                }
                else {
                    if( !@$parent->children ) {
                        $parent->children = array();
                    }
                    $child = $parent->children[] = new stdClass;
                    $child->content = $content;
                    $child->parent = $parent;
                    $parent = $child;
                }        
            }
            else {                
                $depth--;
    
                if( @$parent->parent ) {
                    $parent = $parent->parent;
                }
    
                if( @$content ) {
                    $parent->endContent = $content;
                }                
    
            }
    
        }
    
    
        foreach( $root->children as $node ) {
            $ret .= get_node_contents( $node );
        }
    
        $ret .= substr( $str, $index, $len - $index );
    
    
        return $ret;
    
    }
    
    echo reduce_depth( "asdasdas[code]l[/code][code]a[code]lol[/code][code]b[code]c[code]d[code]e[code]f[code]g[/code][/code][/code][/code][/code][/code][/code]aasdasdsasd", 4 ). "\n";
    echo reduce_depth( "[code]a[code]b[code]c[code]d[code]e[code]f[code]g[/code][/code][/code][/code][/code][/code][/code]", 4 ) . "\n";
    echo reduce_depth( "[code]a[code]b[code]c[code]d[code]TEST[/code][code]e[code]f[code]g[/code][/code][/code][/code][/code][/code][/code]", 4 ) . "\n";
    echo reduce_depth("[code][code]bugi[/code]bugi2[/code]", 1) . "\n"; 
    echo reduce_depth("[code][code]bugi[/code]bugi2[code]bugi3[/code]bugi4[code]bugi5[/code]bugi6[/code]", 3) . "\n"; 
    
    
    /*
        asdasdas[code]l[/code][code]a[code]lol[/code][code]b[code]c[code]d[code]e[/code][/code][/code][/code][/code]aasdasdsasd
        [code]a[code]b[code]c[code]d[code]e[/code][/code][/code][/code][/code]
        [code]a[code]b[code]c[code]d[code]TEST[/code][code]e[/code][/code][/code][/code][/code]
        [code][code]bugi[/code]bugi2[/code]
        [code][code]bugi[/code][code]bugi3[/code][code]bugi5[/code]bugi6[/code]
    
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have table with column 'URL' whrere I store urls like this
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say that I have a set of relations that looks like this: relations
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have some json like this in mongo: {n:5} and a java
Let's say I have an string variable called *magic_string* which value is set to

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.