function pleac_Printing_a_List_with_Commas() { function commify_series($list) { $n = str_word_count($list); $series = str_word_count($list, 1); if ($n == 0) { return NULL; } if ($n == 1) { return $series[0]; } if ($n == 2) { return $series[0] . ' and ' . $series[1]; } return join(', ', array_slice($series, 0, -1)) . ', and ' . $series[$n - 1]; } // ------------ echo commify_series('red') . "\n"; echo commify_series('red yellow') . "\n"; echo commify_series('red yellow green') . "\n"; $mylist = 'red yellow green'; echo 'I have ' . commify_series($mylist) . " marbles.\n"; // ---------------------------- function commify_series($arr) { $n = count($arr); $sepchar = ','; foreach ($arr as $str) { if (strpos($str, ',') === false) { continue; } $sepchar = ';'; break; } if ($n == 0) { return NULL; } if ($n == 1) { return $arr[0]; } if ($n == 2) { return $arr[0] . ' and ' . $arr[1]; } return join("{$sepchar} ", array_slice($arr, 0, -1)) . "{$sepchar} and " . $arr[$n - 1]; } // ------------ $lists = array(array('just one thing'), split(' ', 'Mutt Jeff'), split(' ', 'Peter Paul Mary'), array('To our parents', 'Mother Theresa', 'God'), array('pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna'), array('recycle tired, old phrases', 'ponder big, happy thoughts'), array('recycle tired, old phrases', 'ponder big, happy thoughts', 'sleep and dream peacefully')); foreach ($lists as $arr) { echo 'The list is: ' . commify_series($arr) . ".\n"; } }
function pleac_Putting_Commas_in_Numbers() { // PHP offers the 'number_format' built-in function to, among many other format tasks, // commify numbers. Perl-compatible [as well as extended] regexes are also available function commify_series($s) { return number_format($s, 0, '', ','); } // ------------ $hits = 3456789; printf("Your website received %s accesses last month\n", commify_series($hits)); // ---------------------------- function commify($s) { return strrev(preg_replace('/(\\d\\d\\d)(?=\\d)(?!\\d*\\.)/', '${1},', strrev($s))); } // ------------ $hits = 3456789; echo commify(sprintf("Your website received %d accesses last month\n", $hits)); }