JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
format_int() removes leading zeros
[wfpl.git] / format.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file contains basic encodings
24
25 function format_int($str) {
26         $str = ereg_replace('[^0-9]', '', $str);
27         return ereg_replace('^0*([1-9])', '\1', $str);
28 }
29
30 function format_oneline($str) {
31         $str = str_replace("\r", '', $str);
32         return str_replace("\n", '', $str);
33 }
34
35 function format_unix($str) {
36         return unix_newlines($str);
37 }
38
39 function format_yesno($str) {
40         if($str) {
41                 return "Yes";
42         } else {
43                 return "No";
44         }
45 }
46
47 function format_email($str) {
48         # FIXME
49         return format_oneline($str);
50 }
51
52 function format_url($str) {
53         # FIXME
54         return format_oneline($str);
55 }
56
57 function format_money($str, $display_cents = true) {
58         $str = ereg_replace('[^0-9.]', '', $str);
59         if($display_cents) {
60                 $int = (int)($str * 100);
61                 $cents = $int % 100;
62                 $cents = sprintf('.%02d', $cents);
63                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
64         } else {
65                 $cents = '';
66                 $int = round($str);
67         }
68         $chars = (string)$int;
69         $output = '';
70         $comma = 4;
71         $index = strlen($chars);
72         while($index) {
73                 --$index;
74                 --$comma;
75                 if($comma == 0) {
76                         $comma = 3;
77                         $output = ',' . $output;
78                 }
79                 $char = substr($chars, $index, 1);
80                 $output = $char . $output;
81         }
82         $output = '$' . $output . $cents;
83         return $output;
84 }
85
86 function format_dollars($str) {
87         return format_money($str, false);
88 }
89
90 function format_phone($str) {
91         $str = ereg_replace('[^0-9]', '', $str);
92         $str = ereg_replace('^1*', '', $str);
93         $len = strlen($str);
94         $output = '';
95
96         if($len < 10 && $len != 7) {
97                 #NOT A VALID PHONE NUMBER
98                 return $str;
99         }
100
101         if($len > 10) {
102                 $output = ' ext: ' . substr($str, 10);
103                 $len = 10;
104         }
105
106         if($len == 10) {
107                 $area = substr($str, 0, 3);
108                 $str = substr($str, 3);
109         }
110
111         $output = substr($str, 3) . $output;
112         $output = substr($str, 0, 3) . '-' . $output;
113
114         if($area) {
115                 $output = "($area) " . $output;
116         }
117
118         return $output;
119 }
120
121
122 #function ftest($val) {
123 #       printf("$val: '%s'<br />\n", format_phone($val, true));
124 #}
125 #
126 #echo "FORMAT TESTS<br><br>";
127 #ftest("$3");
128 #ftest("3.99");
129 #ftest("3.5");
130 #ftest("891234");
131 #ftest("8221234");
132 #ftest("82212334");
133 #ftest("122313234");
134 #ftest("1158221234");
135 #ftest("1558221234");
136 #ftest("12235513234");
137 #ftest("122355123334");
138 #ftest("1585552212334");
139 #ftest("15855522123334");
140
141 ?>