note 51907 added to function.implode
| From: | davidpk212 at gmail dot com | Date: | Thu, 14 Apr 2005 15:36:45 +0000 |
| Subject: | note 51907 added to function.implode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88018@lists.php.net to get a copy of this message | ||
I made this function to create an english-readable list from an array.
<?php
function english_list($array, $oxfordcomma=1) {
$count = count($array)-1;
$last = $array[$count];
unset($array[$count]);
$str = join(", ", $array);
if ($oxfordcomma) {
$str .= ",";
}
$str .= " and $last";
return $str;
}
?>
The optional parameter "oxfordcomma" indicates whether or not to use the Oxford comma (a
comma before the "and").
Example:
<?php
print english_list(array("foo", "bar", "foobar", "barfoo"));
?>
Would produce:
foo, bar, foobar, and barfoo
----
Manual Page -- http://www.php.net/manual/en/function.implode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51907
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51907&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51907&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51907&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51907&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51907&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51907&report=yes
Search -- http://master.php.net/manage/user-notes.php