php将文本列表转换为html列表

php将文本列表转换为html列表,php,preg-replace,carriage-return,Php,Preg Replace,Carriage Return,我有一个表列,其中包含由“br”标记分隔的项目列表(没有新行) 我获取这些数据并将br标记转换为新行,如下所述 $nlformat = str_replace("<br>","\n",$mem['mem_services']); 我对这个问题的态度是, 我有一个介于\n char之间的字符串。我将首先用它构建一个格式化数组,然后用它构建html。这样您就可以使用与您的问题相关的代码部分 请这边试试 $nlFormat="item\nitem\nitem\n\nitem\n

我有一个表列,其中包含由“br”标记分隔的项目列表(没有新行)

我获取这些数据并将br标记转换为新行,如下所述

$nlformat = str_replace("<br>","\n",$mem['mem_services']);

我对这个问题的态度是, 我有一个介于\n char之间的字符串。我将首先用它构建一个格式化数组,然后用它构建html。这样您就可以使用与您的问题相关的代码部分

请这边试试

$nlFormat="item\nitem\nitem\n\nitem\nitem\nitem";
$tmp_arr1= explode("\n\n",$nlFormat);
$final_arr=array();
// now $tmp_arr1 has n number of sections;
foreach($tmp_arr1 as $section){
    $final_arr[]= explode("\n",$section);
}

//var_dump($final_arr);
现在让我们生成html

$html="";
foreach($final_arr as $section){
    $html.="<ul><li>";
    $html.=implode("</li><li>",$section);
    $html.="</li></ul>";
}
echo $html;
$html=”“;
foreach($final_arr as$section){
$html.=“
  • ”; $html.=内爆(“
  • ”,$section); $html.=“
”; } echo$html;
你得到了什么而不是期望的结果?它对我有效:@Barmar-有趣的是,它在你提到我的测试站点上有效,因为我尝试了你使用的代码并上传到我的服务器,但它不起作用。可能是混淆了nix/windows EOL,尝试更新preg_replace。看起来您的regexp可以处理所有EOL格式,因此应该可以工作。我看到您的建议有一些潜力,因为我现在可以生成两个数组。我只需要使用它,看看是否可以生成两个html列表。我还没有尝试运行此代码,请告诉我这是否对您有帮助,谢谢你的帮助。非常感谢,因为我不想提及我在preg_replace()上花了多少时间。所以我去掉了var_转储,并在$finalarray[]vaiable的末尾添加了一个分号。工作起来很有魅力!还要提到的是,这是一种非常创新的方法。再次感谢。
item
item
item

item
item
item
$nlFormat="item\nitem\nitem\n\nitem\nitem\nitem";
$tmp_arr1= explode("\n\n",$nlFormat);
$final_arr=array();
// now $tmp_arr1 has n number of sections;
foreach($tmp_arr1 as $section){
    $final_arr[]= explode("\n",$section);
}

//var_dump($final_arr);
$html="";
foreach($final_arr as $section){
    $html.="<ul><li>";
    $html.=implode("</li><li>",$section);
    $html.="</li></ul>";
}
echo $html;