1

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

2
  • You might be better off using a proven library like markdown instead of writing your own parser. Commented Feb 4, 2015 at 1:45
  • @jeroen I thought about it, but It's only going to be an un-ordered list and an ordered list so I thought I'd avoid the overkill with a simple block! Commented Feb 4, 2015 at 1:52

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
        }
    }
?>

Comments

0

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);

Comments

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.