从 php 中的字符串格式化 HTML 列表
Formating a HTML list from a string in php
我正在创建一个论坛,用户可以在其中输入他可以使用特定方法格式化的文本。我已经能够使用 substring() 完成大部分字符串工作,但我在处理有序列表和无序列表时遇到了问题。
以无序为例
用户输入将是:
Example of text and here is my UL:
* Element 1
* Element 2
* Element 3
* Element 4
* Element 5
Thank you. Another one:
* Another 1
* Another 2
它将像这样进入数据库,然后我想在 php 中解决这个问题以获得以下输出:
Example of text and here is my UL:
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
<li>Element 4</li>
<li>Element 5</li>
<ul>
Thank you. Another one:
<ul>
<li>Another 1</li>
<li>Another 2</li>
<ul>
这里的问题是我知道如何替换 <li>
的“*”,但我不知道如何找到前 5 个,然后是另外 2 个,这样他们都可以拥有自己的他们周围的 ul 标签 <ul></ul>
。
我在我的函数开始时使用了 bl2br() 所以马车都是
这里是我的功能的一部分,为了提供帮助而被删减了一些:
function String_ToOutput($String_Output){
//Replace Cariage with HTML Code
$Temp_String = nl2br($String_Output);
//List Code
while(($pos = strpos($Temp_String, "\n*")) !== false){
$Temp_String = substr($Temp_String, 0, $pos) . "<ul><li>" . substr($Temp_String, $pos + 3);
$pos = strpos($Temp_String, "\n", $pos);
while((substr($Temp_String, $pos+1, 1)) == "*"){
$Next = strpos($Temp_String, "\n", $pos);
$Temp_String = substr($Temp_String, 0, $pos) . "<li>" . substr($Temp_String, $pos + 2);
$pos = $Next;
}
}
return $Temp_String;
}
感谢您的帮助
你能不使用 Markdown 语法吗?这样你就不必重新发明轮子了。
试试这个代码。
function String_ToOutput($String_Output){
//Replace Cariage with HTML Code
$Temp_String = nl2br($String_Output);
$lines=explode("\n",$Temp_String);
$start_list=false;
foreach($lines as &$line){
if(strpos($line,'*')!==False){
if(!$start_list)
$line="<ul> ".$line;
$line=str_replace('*',"<li>",$line)."</li>";
$start_list=true;
}
else{
if($start_list){
$start_list=false;
$line="</ul> ". $line;
}
}
//echo $line;
}
$sring=implode("\n",$lines);
return $sring;
}
我正在创建一个论坛,用户可以在其中输入他可以使用特定方法格式化的文本。我已经能够使用 substring() 完成大部分字符串工作,但我在处理有序列表和无序列表时遇到了问题。
以无序为例
用户输入将是:
Example of text and here is my UL:
* Element 1
* Element 2
* Element 3
* Element 4
* Element 5
Thank you. Another one:
* Another 1
* Another 2
它将像这样进入数据库,然后我想在 php 中解决这个问题以获得以下输出:
Example of text and here is my UL:
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
<li>Element 4</li>
<li>Element 5</li>
<ul>
Thank you. Another one:
<ul>
<li>Another 1</li>
<li>Another 2</li>
<ul>
这里的问题是我知道如何替换 <li>
的“*”,但我不知道如何找到前 5 个,然后是另外 2 个,这样他们都可以拥有自己的他们周围的 ul 标签 <ul></ul>
。
我在我的函数开始时使用了 bl2br() 所以马车都是
这里是我的功能的一部分,为了提供帮助而被删减了一些:
function String_ToOutput($String_Output){
//Replace Cariage with HTML Code
$Temp_String = nl2br($String_Output);
//List Code
while(($pos = strpos($Temp_String, "\n*")) !== false){
$Temp_String = substr($Temp_String, 0, $pos) . "<ul><li>" . substr($Temp_String, $pos + 3);
$pos = strpos($Temp_String, "\n", $pos);
while((substr($Temp_String, $pos+1, 1)) == "*"){
$Next = strpos($Temp_String, "\n", $pos);
$Temp_String = substr($Temp_String, 0, $pos) . "<li>" . substr($Temp_String, $pos + 2);
$pos = $Next;
}
}
return $Temp_String;
}
感谢您的帮助
你能不使用 Markdown 语法吗?这样你就不必重新发明轮子了。
试试这个代码。
function String_ToOutput($String_Output){
//Replace Cariage with HTML Code
$Temp_String = nl2br($String_Output);
$lines=explode("\n",$Temp_String);
$start_list=false;
foreach($lines as &$line){
if(strpos($line,'*')!==False){
if(!$start_list)
$line="<ul> ".$line;
$line=str_replace('*',"<li>",$line)."</li>";
$start_list=true;
}
else{
if($start_list){
$start_list=false;
$line="</ul> ". $line;
}
}
//echo $line;
}
$sring=implode("\n",$lines);
return $sring;
}