A regex that converts text lists to html in PHP

后端 未结 4 2104
谎友^
谎友^ 2021-01-05 08:51

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

* List item
* Another list item

to html:

相关标签:
4条回答
  • 2021-01-05 08:55

    Well, you could simply do

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

    or, if you really want to use preg_replace

    $text = preg_replace("/(\<li\>(.*?)\<\/li\>\n*)+/is","<ul>\n$1\n</ul>\n",$text);
    
    0 讨论(0)
  • 2021-01-05 08:58

    Why don't you store the first regex in an array with preg_match_all, and glue it like this:

    $list='<ul><li>';
    $list .= implode('</li><li>',$arr_regex);
    $list .= '</li></ul>';
    
    0 讨论(0)
  • 2021-01-05 08:59

    Perhaps you may find PHP Markdown useful.

    0 讨论(0)
  • 2021-01-05 09:05

    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.

    0 讨论(0)
提交回复
热议问题