From 1dc6c1128b69393d05212e53d41a92cba285ff8e Mon Sep 17 00:00:00 2001 From: nate Date: Thu, 27 Jul 2006 04:20:30 +0000 Subject: [PATCH] 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 --- cake/libs/view/helpers/text.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cake/libs/view/helpers/text.php b/cake/libs/view/helpers/text.php index 0a006c07a..24719ca0d 100644 --- a/cake/libs/view/helpers/text.php +++ b/cake/libs/view/helpers/text.php @@ -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. *