note 51419 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:06:57 +0000 |
| Subject: | note 51419 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195157@lists.php.net to get a copy of this message | ||
Note Submitter: webkami [at] gmail dout com
----
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".
function nl2li($str,$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
// add last list item ending tag
$str = "<$tag $tag_type><li>" . $str ."</li></$tag>";
//replace /n with adding two tags
// add previous list item ending tag
// add next list item starting tag
$str = str_replace("\n","</li><br />\n<li>",$str);
//spit back the modified string
return $str;
}