Convert txt-list to HTML-list

The following PHP code converts a txt list to at html list. Look at example below. Nothing new in this… alot of Wiki pages can do that, I just thought it was cool. After using alot of time to figure out how (new to regular expressions) it makes you wanna show it to the world:

 * This function isn't supposed to be called directly
 * But through encodeList()

function encodeListElements($text) {
    return preg_replace("/^\s?(([\*|\-]) (.*?))\n/m", "
<li>\\3</li>", $text);

function encodeList($text) {
    return preg_replace("/\n((\s?(([\*|\-]) (.*?))\n)+)/sie", "'
<ul>' . encodeListElements('$1') .'</ul>'", $text);

Note: Your browser might split up the code lines, so make a double check.



* Milk
* Water
* Coca Cola


<li>Coca Cola</li>
Creative Commons License
Convert txt-list to HTML-list by Jacob Emcken is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.