JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
format_url() now adds http:// if there's no :
[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 = str_replace('_', ' ', $str);
26         $str = ucwords($str);
27         return str_replace('Email', 'E-mail', $str);
28 }
29
30 # This function makes sure that $str is in the list of options, and returns "" otherwise
31 function format_options($str, $name) {
32         if(!isset($GLOBALS[$name . '_options'])) {
33                 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
34         }
35
36         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
37                 list($key, $value) = $keyval;
38                 if($str == $key) {
39                         return $str;
40                 }
41         }
42
43         return $str;
44 }
45
46 function format_int($str) {
47         $str = ereg_replace('[^0-9]', '', $str);
48         return ereg_replace('^0*([0-9])', '\1', $str);
49 }
50
51 function format_decimal($str) {
52         $str = ereg_replace('[^0-9.]', '', $str);
53         $pos = strpos($str, '.');
54         if($pos !== false) {
55                 $str = str_replace('.', '', $str);
56                 if($pos == 0) {
57                         return '0.' . $str;
58                 } elseif($pos == strlen($str)) {
59                         return $str;
60                 } else {
61                         return substr($str, 0, $pos) . '.' . substr($str, $pos);
62                 }
63         }
64         return $str;
65 }
66
67 # return 0 of there's no digits
68 function format_int_0($str) {
69         $str = format_int($str);
70         if($str == '') {
71                 return '0';
72         }
73         return $str;
74 }
75
76 function format_zip($str) {
77         $str = ereg_replace('[^0-9]', '', $str);
78         if(strlen($str) > 5) {
79                 return substr($str, 0, 5) . '-' . substr($str, 5);
80         }
81         return $str;
82 }
83
84 function format_filename($str, $allow_uppercase = false) {
85         if(!$allow_uppercase) {
86                 $str = strtolower($str);
87         }
88         $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
89         return ereg_replace('^[.-]', '_', $str);
90 }
91
92 function client_path_to_filename($path) {
93         $filename = ereg_replace(".*[:/\\]", '', $path);
94         return format_filename($filename, true);
95 }
96
97
98 function format_h_w_image($str) {
99         $fields = explode(' ', $str);
100         if(count($fields) != 3) {
101                 return '';
102         }
103
104         list($width, $height, $filename) = $fields;
105         $width = format_int_0($width);
106         $height = format_int_0($height);
107         $filename = format_filename($filename);
108
109         return "$width $height $filename";
110 }
111
112 function format_varname($str) {
113         $str = strtolower($str);
114         $str = ereg_replace('[^a-z0-9_]', '_', $str);
115         return ereg_replace('^[0-9]*', '', $str);
116 }
117
118 function format_oneline($str) {
119         $str = str_replace("\r", '', $str);
120         return str_replace("\n", '', $str);
121 }
122
123 function format_unix($str) {
124         return unix_newlines($str);
125 }
126
127 function format_bool($str) {
128         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
129                 return 1;
130         } else {
131                 return 0;
132         }
133 }
134
135 function format_yesno($str) {
136         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
137                 return 'Yes';
138         } else {
139                 return 'No';
140         }
141 }
142
143 function format_email($str) {
144         # FIXME
145         return trim(format_oneline($str));
146 }
147
148 function format_url($str) {
149         # FIXME check for TLD? encode special chars?
150         $str = trim(format_oneline($str));
151         if($str !== '') {
152                 if(strpos($str, ':') === false) {
153                         $str = 'http://' . $str;
154                 }
155         }
156         return $str;
157 }
158
159 function format_money($str, $display_cents = true) {
160         $str = ereg_replace('[^0-9.]', '', $str);
161         if($display_cents) {
162                 $int = (int)($str * 100);
163                 $cents = $int % 100;
164                 $cents = sprintf('.%02d', $cents);
165                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
166         } else {
167                 $cents = '';
168                 $int = round($str);
169         }
170         $chars = (string)$int;
171         $output = '';
172         $comma = 4;
173         $index = strlen($chars);
174         while($index) {
175                 --$index;
176                 --$comma;
177                 if($comma == 0) {
178                         $comma = 3;
179                         $output = ',' . $output;
180                 }
181                 $char = substr($chars, $index, 1);
182                 $output = $char . $output;
183         }
184         $output = '$' . $output . $cents;
185         return $output;
186 }
187
188 function format_dollars($str) {
189         return format_money($str, false);
190 }
191
192 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
193 function format_mdy_to_ymd($str) {
194         require_once('code/wfpl/time.php');
195         return mdy_to_ymd(format_oneline($str));
196 }
197
198 function format_phone($str) {
199         $str = ereg_replace('[^0-9]', '', $str);
200         $str = ereg_replace('^1*', '', $str);
201         $len = strlen($str);
202         $output = '';
203
204         if($len < 10 && $len != 7) {
205                 #NOT A VALID PHONE NUMBER
206                 return $str;
207         }
208
209         if($len > 10) {
210                 $output = ' ext: ' . substr($str, 10);
211                 $len = 10;
212         }
213
214         if($len == 10) {
215                 $area = substr($str, 0, 3);
216                 $str = substr($str, 3);
217         }
218
219         $output = substr($str, 3) . $output;
220         $output = substr($str, 0, 3) . '-' . $output;
221
222         if($area) {
223                 $output = "($area) " . $output;
224         }
225
226         return $output;
227 }
228
229 ?>