0

I have a textarea field. The output will be a HTML list. Each line will be a new list item. If a user type in a hypen ('-') then the list item will be nested

Sample
test1
te-st2
-test3
-test4
--test5
--test6
-test7
test8
-test9
test10

Output should be
Array (test1, 
       te-st2,
       array(test3, 
             test4,
             array(test5, test6),
             test7
       ),
      test8,
      array(test9),
      test10

I am not worry about the key values. I then run theme_item_list from http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list To create the list

1
  • will this list only be listed to 3 or is it n? Commented Mar 23, 2011 at 16:57

1 Answer 1

1

try this (this has not been tested so it may need a little tweaking):

 $sample = "test1
te-st2
-test3
-test4
--test5
--test6
-test7
test8
-test9
test10"

$arr = explode("\n",$sample);
foreach($arr as $key=>$val){
    if($val[0] == '-'){
        unset($val[0]);
        if($val[1] == '-'){
            unset($val[0]);
            unset($arr[$key]);
            $arr[$key-1][] = $val;
        }
        else {
            $arr[$key] = array($val);
        }      
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes. sorry. u have to use a string replace of the first letter instead of that. as i said above this was not rly tested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.