| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4 // +----------------------------------------------------------------------+ 5 // | Akelos Framework - http://www.akelos.org | 6 // +----------------------------------------------------------------------+ 7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez | 8 // | Released under the GNU Lesser General Public License, see LICENSE.txt| 9 // +----------------------------------------------------------------------+ 10 11 /** 12 * @package ActionView 13 * @subpackage Helpers 14 * @author Bermi Ferrer <bermi a.t akelos c.om> 15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org 16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html> 17 */ 18 19 20 /** 21 * Provides methods for converting a number into a formatted string that currently represents 22 * one of the following forms: phone number, percentage, money, or precision level. 23 */ 24 class NumberHelper extends AkObject 25 { 26 /** 27 * Formats a +number+ into a US phone number string. The +options+ can be a array used to customize the 28 * format of the output. 29 * The area code can be surrounded by parentheses by setting +area_code+ to true; default is false 30 * The delimiter can be set using +delimiter+; default is "-" 31 * Examples: 32 * $number_helper->number_to_phone(1235551234) => 123-555-1234 33 * $number_helper->number_to_phone(1235551234, array('area_code' => true)) => (123) 555-1234 34 * $number_helper->number_to_phone(1235551234, array('delimiter' => " ")) => 123 555 1234 35 * $number_helper->number_to_phone(1235551234, array('area_code' => true, 'extension' => 555)) => (123) 555-1234 x 555 36 */ 37 function number_to_phone($number, $options = array()) 38 { 39 $default_options = array( 40 'area_code'=>false, 41 'delimiter' => '-', 42 'extension'=> '', 43 'extension_delimiter' => 'x' 44 ); 45 46 $options = array_merge($default_options, $options); 47 48 $number = $options['area_code'] == true ? preg_replace( 49 '/([0-9]{3})([0-9]{3})([0-9]{4})/',"(\\1) \\2{$options['delimiter']}\\3",$number) : 50 preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/',"\\1{$options['delimiter']}\\2{$options['delimiter']}\\3",$number); 51 52 return empty($options['extension']) ? $number : "$number {$options['extension_delimiter']} {$options['extension']}"; 53 54 } 55 56 /** 57 * Formats a +number+ into a currency string. The +options+ array can be used to customize the format of the output. 58 * The +number+ can contain a level of precision using the +precision+ key; default is 2 59 * The currency type can be set using the +unit+ key; default is "$" 60 * The unit separator can be set using the +separator+ key; default is "." 61 * The delimiter can be set using the +delimiter+ key; default is "," 62 * Examples: 63 * $number_helper->number_to_currency(1234567890.50) => $1,234,567,890.50 64 * $number_helper->number_to_currency(1234567890.506) => $1,234,567,890.51 65 * $number_helper->number_to_currency(1234567890.50, 66 * array('unit' => "£", 'separator' => ",", 'delimiter' => "")) => £1234567890,50 67 * $number_helper->number_to_currency(1234567890.50, 68 * array('unit' => " €", 'separator' => ",", 'delimiter' => ".", 69 * 'unit_position' => 'right')) => 1.234.567.890,50 € 70 */ 71 72 function number_to_currency($number, $options = array()) 73 { 74 $default_options = Ak::locale('currency'); 75 76 $options = array_merge($default_options, $options); 77 78 return 79 ($options['unit_position'] == 'left' ? $options['unit'] : ''). 80 number_format($number, $options['precision'],$options['separator'], $options['delimiter']). 81 ($options['unit_position'] == 'right' ? $options['unit'] : ''); 82 83 } 84 85 86 /** 87 * Formats a +number+ as into a percentage string. The +options+ array can be used to customize the format of 88 * the output. 89 * The +number+ can contain a level of precision using the +precision+ key; default is 2 90 * The unit separator can be set using the +separator+ key; default is "." 91 * Examples: 92 * $number_helper->number_to_percentage(100) => 100.00% 93 * $number_helper->number_to_percentage(100, array('precision' => 0)) => 100% 94 * $number_helper->number_to_percentage(302.0576, array('precision' => 3)) => 302.058% 95 */ 96 function number_to_percentage($number, $options = array()) 97 { 98 99 $default_options = array( 100 'precision'=>2, 101 'separator' => '.', 102 ); 103 104 $options = array_merge($default_options, $options); 105 106 return number_format($number, $options['precision'],$options['separator'],'').'%'; 107 } 108 109 /** 110 * Formats a +number+ with a +delimiter+. 111 * Example: 112 * $number_helper->number_with_delimiter(12345678) => 12,345,678 113 */ 114 function number_with_delimiter($number, $delimiter=',') 115 { 116 return preg_replace('/(\d)(?=(\d\d\d)+(?!\d))/', "\\1{$delimiter}", $number); 117 } 118 119 /* 120 * Returns a formatted-for-humans file size. 121 * 122 * Examples: 123 * $number_helper->human_size(123) => 123 Bytes 124 * $number_helper->human_size(1234) => 1.2 KB 125 * $number_helper->human_size(12345) => 12.1 KB 126 * $number_helper->human_size(1234567) => 1.2 MB 127 * $number_helper->human_size(1234567890) => 1.1 GB 128 */ 129 130 function number_to_human_size($size, $decimal = 1) 131 { 132 if(is_numeric($size )){ 133 $position = 0; 134 $units = array( ' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'); 135 while( $size >= 1024 && ( $size / 1024 ) >= 1 ) { 136 $size /= 1024; 137 $position++; 138 } 139 return round( $size, $decimal ) . $units[$position]; 140 }else { 141 return '0 Bytes'; 142 } 143 } 144 function human_size($size) 145 { 146 return NumberHelper::number_to_human_size($size); 147 } 148 149 function human_size_to_bytes($size) 150 { 151 $units = array('BYTE','KB','MB','GB','TB'); 152 $size = str_replace(array('BYTE','KILOBYTE','MEGABYTE','GIGABYTE','TERABYTE'), $units, rtrim(strtoupper($size),'S')); 153 if(preg_match('/([0-9\.]+) ?('.join('|',$units).')/', $size, $match)){ 154 return intval(ceil((double)$match[1] * pow(1024,array_search($match[2], $units)))); 155 } 156 return 0; 157 } 158 159 160 /** 161 * Formats a +number+ with a level of +precision+. 162 * Example: 163 * $number_helper->number_with_precision(111.2345) => 111.235 164 */ 165 166 function number_with_precision($number, $precision=3) 167 { 168 /** 169 * @todo fix number rounding. Precision on linux boxes rounds to the lower (Mac and Windows work as the example) 170 */ 171 if(strstr($number,'.')){ 172 $decimal = round(substr($number,strpos($number,'.')+1),$precision+1); 173 if(substr($decimal , -1) == 5){ 174 $number = substr($number,0,strpos($number,'.')) .'.'. ($decimal+1); 175 } 176 } 177 178 return round( $number, $precision ); 179 } 180 181 182 /** 183 * Add zeros to the begining of the string until it reaches desired length 184 * Example: 185 * $number_helper->zeropad(123, 6) => 000123 186 */ 187 function zeropad($number, $length) 188 { 189 return str_pad($number, $length*-1, '0'); 190 } 191 192 } 193 194 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |