The following will convert straight into HTML without recursion:
$text = array();
$text[] = '- Item 1';
$text[] = '-- Item 1.1';
$text[] = '-- Item 1.2';
$text[] = '--- Item 1.2.1';
$text[] = '--- Item 1.2.2';
$text[] = '---- Item 1.2.2.1';
$text[] = '- Item 2';
$text[] = '- Item 3';
$text[] = '- Item 4';
$text[] = '-- Item 4.1';
$text[] = '-- Item 4.2';
$previous_dash_count = 0; // topmost parent
foreach ($text as $line) {
if (preg_match('/(^\-+)(.*)/', $line, $matches, PREG_OFFSET_CAPTURE)===1) {
$dash_count = strlen($matches[1][0]);
$title = $matches[2][0];
if ($dash_count == $previous_dash_count) {
echo "<li>$title</li>\n";
} elseif ($dash_count > $previous_dash_count) {
echo str_repeat("<ul>\n", $dash_count - $previous_dash_count);
echo "<li>$title</li>\n";
} else {
echo str_repeat("</ul>\n",$previous_dash_count-$dash_count+1);
echo "<ul>\n";
echo "<li>$title</li>\n";
}
$previous_dash_count = $dash_count;
}
}
echo str_repeat("</ul>\n",$previous_dash_count);
I make several assumptions. That the input text always behaves well and doesn't contain randomness. Also I don't assume UTF-8 text, but you're safe with dashes.
Here is the array version in all its gory glory:
$stack = array();
$previous_dash_count = 0;
$parent_node = array();
foreach ($text as $line) {
if (preg_match('/(^\-+)(.*)/', $line, $matches, PREG_OFFSET_CAPTURE)===1) {
$dash_count = strlen($matches[1][0]);
$title = $matches[2][0];
if ($dash_count == $previous_dash_count) {
$parent_node[] = $title;
} elseif ($dash_count > $previous_dash_count) {
for ($push_count = $previous_dash_count; $push_count<$dash_count; $push_count++) {
array_push($stack, $parent_node); // remember node
$new_child = array();
$new_child[] = $title;
$parent_node[] = $new_child;
$parent_node = $new_child;
}
} else {
for ($pop_count = $previous_dash_count; $pop_count >$dash_count; $pop_count--) {
$old_child = $parent_node;
$parent_node = array_pop($stack);
$parent_node[] = $old_child;
}
$parent_node[] = $title;
}
$previous_dash_count = $dash_count;
}
}
for ($pop_count = $previous_dash_count; $pop_count > 0; $pop_count--) {
$old_child = $parent_node;
$parent_node = array_pop($stack);
$parent_node[] = $old_child;
}
print_r($parent_node);
We keep a stack of array nodes, so we have a link between a child and its parent. Note the structure of this code is identical to that for the straight HTML version.