JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
db_printf()'s %s, dwt_append(), states pulldown, tem_top_subs()
[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_zip($str) {
30         return ereg_replace('[^0-9]', '', $str);
31 }
32
33 function format_filename($str) {
34         $str = strtolower($str);
35         $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
36         return ereg_replace('^[0-9.-]*', '', $str);
37 }
38
39 function format_varname($str) {
40         $str = strtolower($str);
41         $str = ereg_replace('[^a-z0-9_]', '_', $str);
42         return ereg_replace('^[0-9]*', '', $str);
43 }
44
45 function format_oneline($str) {
46         $str = str_replace("\r", '', $str);
47         return str_replace("\n", '', $str);
48 }
49
50 function format_unix($str) {
51         return unix_newlines($str);
52 }
53
54 function format_yesno($str) {
55         if($str) {
56                 return "Yes";
57         } else {
58                 return "No";
59         }
60 }
61
62 function format_email($str) {
63         # FIXME
64         return format_oneline($str);
65 }
66
67 function format_url($str) {
68         # FIXME
69         return format_oneline($str);
70 }
71
72 function format_money($str, $display_cents = true) {
73         $str = ereg_replace('[^0-9.]', '', $str);
74         if($display_cents) {
75                 $int = (int)($str * 100);
76                 $cents = $int % 100;
77                 $cents = sprintf('.%02d', $cents);
78                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
79         } else {
80                 $cents = '';
81                 $int = round($str);
82         }
83         $chars = (string)$int;
84         $output = '';
85         $comma = 4;
86         $index = strlen($chars);
87         while($index) {
88                 --$index;
89                 --$comma;
90                 if($comma == 0) {
91                         $comma = 3;
92                         $output = ',' . $output;
93                 }
94                 $char = substr($chars, $index, 1);
95                 $output = $char . $output;
96         }
97         $output = '$' . $output . $cents;
98         return $output;
99 }
100
101 function format_dollars($str) {
102         return format_money($str, false);
103 }
104
105 function format_phone($str) {
106         $str = ereg_replace('[^0-9]', '', $str);
107         $str = ereg_replace('^1*', '', $str);
108         $len = strlen($str);
109         $output = '';
110
111         if($len < 10 && $len != 7) {
112                 #NOT A VALID PHONE NUMBER
113                 return $str;
114         }
115
116         if($len > 10) {
117                 $output = ' ext: ' . substr($str, 10);
118                 $len = 10;
119         }
120
121         if($len == 10) {
122                 $area = substr($str, 0, 3);
123                 $str = substr($str, 3);
124         }
125
126         $output = substr($str, 3) . $output;
127         $output = substr($str, 0, 3) . '-' . $output;
128
129         if($area) {
130                 $output = "($area) " . $output;
131         }
132
133         return $output;
134 }
135
136 ?>