Skip to content

Instantly share code, notes, and snippets.

@maor
Last active December 18, 2015 05:59
Show Gist options
  • Save maor/5736517 to your computer and use it in GitHub Desktop.
Save maor/5736517 to your computer and use it in GitHub Desktop.
[hello][/hello]
<?php
function count_nested_shortcodes( $shortcode ) {
// each shortcode uses the "[" chatacter twice. The first is of the opening tag, the last is for the closing tag.
// The main shortcode also has 2 occurances. Since we're looking for the nested shortcodes, we'll ignore the main one by subtracting 2 from the total outcome
return (int) ( substr_count( $shortcode, '[' ) / 2 ) - 2;
}
@amitkvint
Copy link

looks good, can you add comments please ?

@amitkvint
Copy link

why are we ignoring the first one? we are looking for the max depth of the array so can we add the The RecursiveIteratorIterator class for example and just spit out the array's depth ?

@amitkvint
Copy link

Maor take a look let me know what did I do wrong :(

$max_depth) { $max_depth = $depth; } } } return $max_depth; } ``` ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment