nl2li – WordPress template tag

Ever wanted to output the contents of a textarea to a html list (e.g. convert New Line to html list) instead of using nl2br ?

This alternative nl2br function (for WordPress) will do it for you (and more).
Just put it in your themes functions.php and your set to go.

It supports multiple columns (CSS styling needed, see below), ordered and unordered lists, list type, list class, container element, container element class.

<?php
/**
* 
* Author: Richard van den Winkel
* Website: http://www.vandenwinkel.com
* 
*/
 
/**
* Retuns (a) html list(s) from a newline seperated string (for example: a textarea list with enters)
* 
* @param mixed $str
* @param mixed $args
*/
function get_nl2li($str, $args = '') 
{
    // default args
    $defaults = array(
        'class' => '', // list class
        'ordered' => 0, // ordered or unordered list
        'type' => '1', // in case of ordered, which type
        'num_columns' => 2, // number of columns (e.g. lists) to generate. CSS styling needed!!!
        'container' => '', // container element
        'container_class' => '' // container element class
    );
 
    // extract the args into local vars (e.g. $class)
    $a = wp_parse_args( $args, $defaults );
    extract( $a, EXTR_SKIP );
 
    // add first and last list item tag
    $str = "<li>" . $str ."</li>";
    // replace /n with adding end and start list item tags
    $str = str_replace("\n","</li>\n<li>",$str);
    // split string by newline character and put elements in array
    $str_exploded = explode("\n", $str);
 
    // init return value
    $ret = '';
 
 
    // list class
    if(strlen($class) > 0) 
    {
        $class = " class=\"$class\"";
    }
 
    // list type
    if($ordered != 0) 
    {
        $tag = 'ol';
        $type = " type=\"$type\"";
        if($num_columns > 0) 
        {
            $start = " start=\"[START]\"";
        }
        else
        {
            $start = '';
        }
    }
    else
    {
        $tag = 'ul';
        $type = '';
        $start = '';
    }
 
    if($num_columns > 1) 
    {
        $num_item_per_list = round(count($str_exploded)/$num_columns, 0);
        $i = 1;
        for($n = 0; $n < $num_columns; $n++)
        {
            $s = str_replace('[START]', $i, $start);
            $ret .= "<{$tag}{$class}{$type}{$s}>";
            for($m = 0; $m < $num_item_per_list; $m++)
            {
                if(array_key_exists($i, $str_exploded)) 
                {
                    $ret .= $str_exploded[$i];
                    $i++;
                }
            }
            $ret .= "</{$tag}>\n";
        }
    }
    else
    {
        $ret = "<{$tag}{$class}{$type}>" . $str . "</{$tag}>\n";
    }
 
 
    // container
    if(strlen($container) > 0) 
    {
        if(strlen($container_class) > 0) 
        {
            $container_class = "class=\"$container_class\"";
        }        
        $ret = "<$container $container_class>" . $ret . "</$container>";
    }
 
    // return the list
    return $ret;
}
 
/**
* echos get_nl2li($str, $args)
* 
* @param mixed $str
* @param mixed $args
*/
function _nl2li($str, $args = '') 
{
    echo get_nl2li($str, $args);
}

A two-column css would look something like this:

    .container {width: 620px; margin: 0px; padding: 0px;}
    ul {width: 285px; margin: 0px 0px 0px 25px; padding: 0px; float: left;}
    li {}

comments

No comments yet.

Sorry, the comment form is closed at this time.