note 76295 added to function.implode

From: Date: Mon, 09 Jul 2007 20:05:17 +0000
Subject: note 76295 added to function.implode
Groups: php.notes 
Request: Send a blank email to php-notes+get-128336@lists.php.net to get a copy of this message
This is my quick function to create a list, English style, but will accept any glue, so you could use 'or', or even 'or though, it could be', etcetera. It should also support PHP 4, although I haven't tested it; it doesn't use the PHP 5 negative substr() trick. <?php /** * Quick script to join items in an array, English * style; using the "one, two and three" style. * * Copyright 2007 Thomas O. Feel free to redistribute * so long as this copyright remains. */ function implode_ea($glue_punct, $glue_word, $array) { // Implode the entire array $result = implode($glue_punct, $array); // Check the length of the array if(count($array) > 1) { // Calculate the amount needed to trim $trimamount = strlen($array[count($array) - 1]) + strlen($glue_punct); // Trim the imploded string $result = substr($result, 0, strlen($result) - $trimamount); // PHP 4 compatible $result = "$result $glue_word " . $array[count($array) - 1]; // Return the result return $result; } else { // In this case, the array cannot be splitted by a // word or punctuation, because it is too small. return $result; } } echo implode_ea(", ", "and", array("one", "two", "three")); ?> (implode_ea stands for 'Implode, English And style') Hope this helps, Tom ---- Server IP: 87.124.64.96 Probable Submitter: 81.86.162.169 ---- Manual Page -- http://www.php.net/manual/en/function.implode.php Edit -- https://master.php.net/note/edit/76295 Del: integrated -- https://master.php.net/note/delete/76295/integrated Del: useless -- https://master.php.net/note/delete/76295/useless Del: bad code -- https://master.php.net/note/delete/76295/bad+code Del: spam -- https://master.php.net/note/delete/76295/spam Del: non-english -- https://master.php.net/note/delete/76295/non-english Del: in docs -- https://master.php.net/note/delete/76295/in+docs Del: other reasons-- https://master.php.net/note/delete/76295 Reject -- https://master.php.net/note/reject/76295 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128336) next »