JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Merge git@github.com:JasonWoof/wfpl
[wfpl.git] / format.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains basic encodings
20
21 function format_caption($str) {
22         $str = str_replace('_', ' ', $str);
23         $str = ucwords($str);
24         return str_replace('Email', 'E-mail', $str);
25 }
26
27 # This function makes sure that $str is in the list of options, and returns "" otherwise
28 function format_options($str, $name) {
29         if(!isset($GLOBALS[$name . '_options'])) {
30                 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
31         }
32
33         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
34                 list($key, $value) = $keyval;
35                 if($str == $key) {
36                         return $str;
37                 }
38         }
39
40         return $str;
41 }
42
43 function format_int($str) {
44         $str = ereg_replace('[^0-9]', '', $str);
45         return ereg_replace('^0*([0-9])', '\1', $str);
46 }
47
48 function format_decimal($str) {
49         $str = ereg_replace('[^0-9.]', '', $str);
50         $pos = strpos($str, '.');
51         if($pos !== false) {
52                 $str = str_replace('.', '', $str);
53                 if($pos == 0) {
54                         return '0.' . $str;
55                 } elseif($pos == strlen($str)) {
56                         return $str;
57                 } else {
58                         return substr($str, 0, $pos) . '.' . substr($str, $pos);
59                 }
60         }
61         return $str;
62 }
63
64 # return 0 of there's no digits
65 function format_int_0($str) {
66         $str = format_int($str);
67         if($str == '') {
68                 return '0';
69         }
70         return $str;
71 }
72
73 function format_zip($str) {
74         $str = ereg_replace('[^0-9]', '', $str);
75         if(strlen($str) > 5) {
76                 return substr($str, 0, 5) . '-' . substr($str, 5);
77         }
78         return $str;
79 }
80
81 function format_filename($str, $allow_uppercase = false) {
82         if(!$allow_uppercase) {
83                 $str = strtolower($str);
84         }
85         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
86         return ereg_replace('^[.-]', '_', $str);
87 }
88
89 function client_path_to_filename($path) {
90         $filename = ereg_replace(".*[:/\\]", '', $path);
91         return format_filename($filename, true);
92 }
93
94
95 function format_h_w_image($str) {
96         $fields = explode(' ', $str);
97         if(count($fields) != 3) {
98                 return '';
99         }
100
101         list($width, $height, $filename) = $fields;
102         $width = format_int_0($width);
103         $height = format_int_0($height);
104         $filename = format_filename($filename);
105
106         return "$width $height $filename";
107 }
108
109 function format_varname($str) {
110         $str = strtolower($str);
111         $str = ereg_replace('[^a-z0-9_]', '_', $str);
112         return ereg_replace('^[0-9]*', '', $str);
113 }
114
115 function format_oneline($str) {
116         $str = str_replace("\r", '', $str);
117         return str_replace("\n", '', $str);
118 }
119
120 function format_unix($str) {
121         return unix_newlines($str);
122 }
123
124 function format_bool($str) {
125         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
126                 return 1;
127         } else {
128                 return 0;
129         }
130 }
131
132 function format_yesno($str) {
133         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
134                 return 'Yes';
135         } else {
136                 return 'No';
137         }
138 }
139
140 function format_email($str) {
141         # FIXME
142         return trim(format_oneline($str));
143 }
144
145 function format_url($str) {
146         # FIXME check for TLD? encode special chars?
147         $str = trim(format_oneline($str));
148         if($str !== '') {
149                 if(strpos($str, ':') === false) {
150                         $str = 'http://' . $str;
151                 }
152         }
153         return $str;
154 }
155
156 function format_money($str, $display_cents = true) {
157         $str = ereg_replace('[^0-9.]', '', $str);
158         if($display_cents) {
159                 $int = (int)($str * 100);
160                 $cents = $int % 100;
161                 $cents = sprintf('.%02d', $cents);
162                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
163         } else {
164                 $cents = '';
165                 $int = round($str);
166         }
167         $chars = (string)$int;
168         $output = '';
169         $comma = 4;
170         $index = strlen($chars);
171         while($index) {
172                 --$index;
173                 --$comma;
174                 if($comma == 0) {
175                         $comma = 3;
176                         $output = ',' . $output;
177                 }
178                 $char = substr($chars, $index, 1);
179                 $output = $char . $output;
180         }
181         $output = '$' . $output . $cents;
182         return $output;
183 }
184
185 function format_dollars($str) {
186         return format_money($str, false);
187 }
188
189 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
190 function format_mdy_to_ymd($str) {
191         require_once('code/wfpl/time.php');
192         return mdy_to_ymd(format_oneline($str));
193 }
194
195 function format_phone($str) {
196         $str = ereg_replace('[^0-9]', '', $str);
197         $str = ereg_replace('^1*', '', $str);
198         $len = strlen($str);
199         $output = '';
200
201         if($len < 10 && $len != 7) {
202                 #NOT A VALID PHONE NUMBER
203                 return $str;
204         }
205
206         if($len > 10) {
207                 $output = ' ext: ' . substr($str, 10);
208                 $len = 10;
209         }
210
211         if($len == 10) {
212                 $area = substr($str, 0, 3);
213                 $str = substr($str, 3);
214         }
215
216         $output = substr($str, 3) . $output;
217         $output = substr($str, 0, 3) . '-' . $output;
218
219         if($area) {
220                 $output = "($area) " . $output;
221         }
222
223         return $output;
224 }
225
226 ?>