Darren
Darren

Reputation: 13128

Modifying array to make <ul> lists

I understand the title is vague, but I need help with this issue. I just can't seem to wrap my head around it.

Basically, I have a textarea where users insert their string/text like this:

Blah blah blah blah blah this is random text whatever it could be blah blah

* Un-ordered list item
* Un-ordered list item

Blah blah blah and here is some more random text because blah blah blah

* Un-ordered list item
* Un-ordered list item

As you can see, users can create a list by adding a * character that a PHP script just modify to generate the appropriate list tags.

What I have so far is this:

$text = array();
$c = 0;
foreach($lines as $line) {
    if(strpos($line, "*") !== FALSE) {
        if($c == 0) {
            $text[] = "<ul>";
        }    
        $text[] = "<li>" . trim(str_replace(array("*", "\n"), '', $line)) . "</li>";
        $c++;
    } else {
        $c = 0;
        $text[] = $line;
    }
}

Which would return something like this:

Array
(
    [0] => Blah blah blah blah blah this is random text whatever it could be blah blah
    [1] =>     
    [2] => <ul>
    [3] => <li>Un-ordered list item</li>
    [4] => <li>Un-ordered list item</li>
    [5] =>     
    [6] =>     Blah blah blah and here is some more random text because blah blah blah
    [7] =>     
    [8] => <ul>
    [9] => <li>Un-ordered list item</li>
    [10] => <li>Un-ordered list item</li>
)

What I need is to be able to close that <ul> tag off after the list is done. As you can see, the user can add as many lists as they desire, so the code block needs to be flexible to accommodate that.

Here's an example of it in action: Example

Upvotes: 1

Views: 52

Answers (3)

Coomie
Coomie

Reputation: 4868

You need to add a marker to remember that an unordered list was started and when you're no longer making the list add in a close for the list. Like this:

$text = array();
$c = 0;
$imalist = false;
foreach($lines as $line) {
    if(strpos($line, "*") !== FALSE) {
        if($c == 0) {
            $text[] = "<ul>";
        }    
        $text[] = "<li>" . trim(str_replace(array("*", "\n"), '', $line)) . "</li>";
        $c++;
    } else {
      if ($c>0){
        $text[] = "</ul>";
      }
      $c = 0;
      $text[] = $line;
    }
}
if ($c>0){
  $text[] = "</ul>";
}

Edit: added closing after the loop in case the last line is a list item.

Upvotes: 1

onegun
onegun

Reputation: 801

just set a flag.

$s = "Blah blah blah blah blah this is random text whatever it could be blah blah

    * Un-ordered list item
    * Un-ordered list item

    Blah blah blah and here is some more random text because blah blah blah

    * Un-ordered list item
    * Un-ordered list item
";

$lines = explode("\n", $s);

$text = array();
$c = 0;
$flag=false; //set flag

foreach($lines as $line) {
    if(strpos($line, "*") !== FALSE) {

        $flag=true;

        if($c == 0) {
            $text[] = "<ul>";
        }    

        $text[] = "<li>" . trim(str_replace(array("*", "\n"), '', $line)) . "</li>";
        $c++;

    } else {
        if($flag==true){
            $text[count($text)-1]=$text[count($text)-1]."</ul>";
            $flag=false;
        }
        $c = 0;
        $text[] = $line;
    }
}

if($flag==true) $text[count($text)-1]=$text[count($text)-1]."</ul>";

print_r($text);

Upvotes: 0

Nathan
Nathan

Reputation: 366

Would this work? It adds a if any list items were previously found.

<?php
    $text = array();
    $c = 0;
    $close=0;
    foreach($lines as $line) {
        if(strpos($line, "*") !== FALSE) {
            if($c == 0) {
                $text[] = "<ul>";
            }    
            $text[] = "<li>" . trim(str_replace(array("*", "\n"), '', $line)) . "</li>";
            $c++;
        } else {
            if($c>0){
                $text[] = "</ul>";
            }
            $c = 0;
            $text[] = $line;
        }
    }
?>

Upvotes: 1

Related Questions