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