修改数组以创建 <ul> 个列表

Modifying array to make <ul> lists

我知道标题含糊不清,但我需要帮助解决这个问题。我似乎无法理解它。

基本上,我有一个文本区域,用户可以像这样插入他们的 string/text:

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

如您所见,用户可以通过添加 * 字符来创建列表,PHP 脚本只需修改该字符即可生成适当的列表标签。

我目前的情况是这样的:

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

哪个 return 像这样:

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

我需要的是能够在列表完成后关闭 <ul> 标记。如您所见,用户可以根据需要添加任意数量的列表,因此代码块需要灵活适应。

这是一个实际的例子:Example

您需要添加一个标记以记住无序列表已启动以及您不再创建该列表时添加该列表的结尾。像这样:

$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>";
}

编辑:在循环后添加关闭以防最后一行是列表项。

这行得通吗?如果之前找到任何列表项,它会添加一个。

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

只需设置一个标志。

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