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