Created
February 13, 2012 14:55
-
-
Save marekkalnik/1817419 to your computer and use it in GitHub Desktop.
nl2li
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could be better, but it's still more readable :)