Adding TextHelper::toList(), to create natural-language lists from arrays

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3304 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-07-27 04:20:30 +00:00
parent c50275de24
commit 1dc6c1128b

View file

@ -189,7 +189,7 @@ class TextHelper extends Helper {
return $this->truncate($text, $radius * 2, $ending);
}
if ($radius < strlen($phrase))
if ($radius < strlen($phrase)) {
$radius = strlen($phrase);
}
@ -208,6 +208,25 @@ class TextHelper extends Helper {
return $excerpt;
}
/**
* Creates a comma separated list where the last two items are joined with 'and', forming natural English
*
* @param array $list The list to be joined
* @return string
*/
function toList($list) {
$r = '';
$c = count($list) - 1;
foreach ($list as $i => $item) {
$r .= $item;
if ($c > 0 && $i < $c)
{
$r .= ($i < $c - 1 ? ', ' : ' and ');
}
}
return $r;
}
/**
* Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
*