PHP Function to convert New Line to List with CSS - nl2li_css

A handy function to convert new line \n seprated text into ordered or unordered list. The CSS friendly version, called nl2li_css().

Copyleft : LGPL
Idea by: webKami
Coded By: webKami

Version 1.0.1





Enhancements:
1, Adds CSS class to list items

A handy function to convert new line \n seprated text into ordered or unordered list. I am calling it nl2li, suggestions welcome. Second optional parameter sets the list as ordered (1) or unordered (0 = default). Third parameter can be used to specify type of ordered list, valid inputs are "1" = default ,"a","A","i","I". Also inputs a param css_class (default ="none") and pass it as class of All <li> List Items.

Code:
here is the CSS friendly version, called nl2li_css()

Inputs a param css_class (default ="none") and pass it as class of All <li> List Items.

<?
function nl2li_css($str,$css_class = "none",$ordered = 0, $type = "1") {

//check if its ordered or unordered list, set tag accordingly
if ($ordered)
{
  
$tag="ol";
  
//specify the type
  
$tag_type="type=$type";
}
else
{   
  
$tag="ul";
  
//set $type as NULL
  
$tag_type=NULL;
}

// add ul / ol tag
// add tag type
// add first list item starting tag - use css class
// add last list item ending tag
$str = "<$tag $tag_type><li class=\"$css_class\">" . $str ."</li></$tag>";

//replace /n with adding two tags
// add previous list item ending tag
// add next list item starting tag - use css class
$str = str_replace("\n","</li><br />\n<li class=\"$css_class\">",$str);

//spit back the modified string
return $str;
}
?>