alexis-j
2/13/2012 - 2:55 PM

nl2li

nl2li

<?php

/**
 * Converts elements divided by newline characters to list items
 * @param String $text
 * @param Array $htmlAttrs
 */
function nl2li($text, array $htmlAttrs = null) {
  if (!empty($htmlAttrs)) {
    $attributes = array_walk($htmlAttrs, function($key, $value) {

      return $key.' = "'.$value.'"';
    });

    $openingLi = '<li '.implode(' ', $attributes).'>';
  }
  else
  {
    $openingLi = '<li>';
  }
  
  $parsedText = '';

  $token = strtok($text, "\n");
  while($token !== false) {
    $parsedText .= $openingLi.$token.'</li>'.PHP_EOL;

    $token = strtok("\n");
  }

  return $parsedText;
}