JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Merge branch 'master' of 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_positive_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 function format_decimal($str) {
65         $str = ereg_replace('[^0-9.-]', '', $str);
66         if(substr($str, 0, 1) == '-') {
67                 $str = format_positive_decimal(substr($str, 1));
68                 if($str !== '' && $str !== '0' && $str != '0.0') {
69                         $str = '-' . $str;
70                 }
71                 return $str;
72         } else {
73                 return format_positive_decimal($str);
74         }
75 }
76
77 # return 0 of there's no digits
78 function format_int_0($str) {
79         $str = format_int($str);
80         if($str == '') {
81                 return '0';
82         }
83         return $str;
84 }
85
86 function format_zip($str) {
87         $str = ereg_replace('[^0-9]', '', $str);
88         if(strlen($str) > 5) {
89                 return substr($str, 0, 5) . '-' . substr($str, 5);
90         }
91         return $str;
92 }
93
94 function format_filename($str, $allow_uppercase = false) {
95         if(!$allow_uppercase) {
96                 $str = strtolower($str);
97         }
98         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
99         return ereg_replace('^[.-]', '_', $str);
100 }
101
102 function client_path_to_filename($path) {
103         $filename = ereg_replace(".*[:/\\]", '', $path);
104         return format_filename($filename, true);
105 }
106
107
108 function format_h_w_image($str) {
109         $fields = explode(' ', $str);
110         if(count($fields) != 3) {
111                 return '';
112         }
113
114         list($width, $height, $filename) = $fields;
115         $width = format_int_0($width);
116         $height = format_int_0($height);
117         $filename = format_filename($filename);
118
119         return "$width $height $filename";
120 }
121
122 function format_varname($str) {
123         $str = strtolower($str);
124         $str = ereg_replace('[^a-z0-9_]', '_', $str);
125         return ereg_replace('^[0-9]*', '', $str);
126 }
127
128 function format_oneline($str) {
129         $str = str_replace("\r", '', $str);
130         return str_replace("\n", '', $str);
131 }
132
133 function format_unix($str) {
134         return unix_newlines($str);
135 }
136
137 function format_bool($str) {
138         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
139                 return 1;
140         } else {
141                 return 0;
142         }
143 }
144
145 function format_yesno($str) {
146         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
147                 return 'Yes';
148         } else {
149                 return 'No';
150         }
151 }
152
153 function format_email($str) {
154         # FIXME
155         return trim(format_oneline($str));
156 }
157
158 function format_url($str) {
159         # FIXME check for TLD? encode special chars?
160         $str = trim(format_oneline($str));
161         if($str !== '') {
162                 if(strpos($str, ':') === false) {
163                         $str = 'http://' . $str;
164                 }
165         }
166         return $str;
167 }
168
169 function format_money($str, $display_cents = true) {
170         $str = ereg_replace('[^0-9.]', '', $str);
171         if($display_cents) {
172                 $int = (int)($str * 100);
173                 $cents = $int % 100;
174                 $cents = sprintf('.%02d', $cents);
175                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
176         } else {
177                 $cents = '';
178                 $int = round($str);
179         }
180         $chars = (string)$int;
181         $output = '';
182         $comma = 4;
183         $index = strlen($chars);
184         while($index) {
185                 --$index;
186                 --$comma;
187                 if($comma == 0) {
188                         $comma = 3;
189                         $output = ',' . $output;
190                 }
191                 $char = substr($chars, $index, 1);
192                 $output = $char . $output;
193         }
194         $output = '$' . $output . $cents;
195         return $output;
196 }
197
198 function format_dollars($str) {
199         return format_money($str, false);
200 }
201
202 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
203 function format_mdy_to_ymd($str) {
204         require_once('code/wfpl/time.php');
205         return mdy_to_ymd(format_oneline($str));
206 }
207
208 function format_phone($str) {
209         $str = ereg_replace('[^0-9]', '', $str);
210         $str = ereg_replace('^1*', '', $str);
211         $len = strlen($str);
212         $output = '';
213
214         if($len < 10 && $len != 7) {
215                 #NOT A VALID PHONE NUMBER
216                 return $str;
217         }
218
219         if($len > 10) {
220                 $output = ' ext: ' . substr($str, 10);
221                 $len = 10;
222         }
223
224         if($len == 10) {
225                 $area = substr($str, 0, 3);
226                 $str = substr($str, 3);
227         }
228
229         $output = substr($str, 3) . $output;
230         $output = substr($str, 0, 3) . '-' . $output;
231
232         if($area) {
233                 $output = "($area) " . $output;
234         }
235
236         return $output;
237 }
238
239 ?>