且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP中将文本列表转换为html的正则表达式

更新时间:2023-02-23 15:15:55

这个问题,如果你在谈论这样一个事实,那就是你使用的代码会在一个ul标签中包含多组li标签,即使假设这里有一个休息时间:

  *第1行
*第1行
*第1行
这不是列表的一部分
*行1
* line 1
* line 1

会变成:

 < ul> 
< li>第1行< / li>
< li>第1行< / li>
< li>第1行< / li>
这不是列表
< li>第1行的一部分< / li>
< li>第1行< / li>
< / ul>

然后我为您解决问题。你有90%的存在,这是我提出的解决方案(但我相信你已经解决了它):

  $ text = preg_replace(/ \ * +(。*)?/ i,< ul>< li> $ 1< / li>< / ul>,$ text); 
$ text = preg_replace(/(\< \ / ul \> \\\
(。*)\< ul \> *)+ /,,$文本);

该解决方案不会混淆文本中任何类别的列表或任何其他内容肯定要分开多个列表。原因是每一个匹配项都会找到星号用来创建文本列表项目的地方,并且用ul和li包围它,然后第二行会找到所有的背对背关闭并打开ul标签并将其删除。


I'm trying to code a regexp to convert a block of text:

* List item
* Another list item

to html:

<ul>
    <li>List item</li>
    <li>Another list item</li>
</ul>

I know there are snippets or classes to do this (Markdown, Textile, etc) but I think it's overkill: I really just want some basic functionality. So far I'm trying with:

$text = preg_replace("/\*+(.*)?/i","<li>$1</li>",$text);

But I don't know how to wrap everything in <ul> tags without using a separate replace, like so:

$text = preg_replace("/(\<li\>(.*)\<\/li\>\n*)+/is","<ul>\n$1\n</ul>\n",$text);

This interferes with other code, for example ordered lists. There must be a better way.

Thanks.

On this question, if you where talking about the fact that the code you used would wrap multiple sets of li tags in one ul tag even if there was suppose to be a break in there like so:

* line 1
* line 1
* line 1
this is not part of a list
* line 1
* line 1
* line 1

Would become:

<ul>
<li>line 1</li>
<li>line 1</li>
<li>line 1</li>
this is not part a the list
<li>line 1</li>
<li>line 1</li>
</ul>

Then I have a solution for you. You had 90% of it there, here is a solution I came up with (but I am sure you already solved it anyway):

$text = preg_replace("/\*+(.*)?/i","<ul><li>$1</li></ul>",$text);
$text = preg_replace("/(\<\/ul\>\n(.*)\<ul\>*)+/","",$text);

The solution does not mess with lists of any kind already on the page in the text or whatever and makes sure to separate multiple lists. Reason is that every match it finds where an asterisk was used to create a text list item it surrounds that with a ul and li then the 2nd line finds all of the back to back closing and opening ul tags and removes them.