3 # Copyright (C) 2005 Jason Woofenden
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.
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.
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/>.
19 # This file contains basic encodings
21 function format_caption($str) {
22 $str = str_replace('_', ' ', $str);
24 return str_replace('Email', 'E-mail', $str);
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().");
33 foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
34 list($key, $value) = $keyval;
43 function format_int($str) {
44 $str = ereg_replace('[^0-9]', '', $str);
45 return ereg_replace('^0*([0-9])', '\1', $str);
48 function format_decimals($str) {
49 $str = ereg_replace('[^0-9]', '', $str);
50 return ereg_replace('^([0-9])0*', '\1', $str);
53 function _format_positive_decimal($str) {
54 $str = ereg_replace('[^0-9.]', '', $str);
55 $pos = strpos($str, '.');
57 $str = str_replace('.', '', $str);
59 return '0.' . format_decimals($str);
60 } elseif($pos == strlen($str)) {
61 return format_int($str);
63 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
66 return format_int($str);
69 function format_positive_decimal($str) {
70 $str = _format_positive_decimal($str);
77 function format_decimal($str) {
78 $str = ereg_replace('[^0-9.-]', '', $str);
79 if(substr($str, 0, 1) == '-') {
80 $str = format_positive_decimal(substr($str, 1));
81 if($str !== '' && $str !== '0') {
86 return format_positive_decimal($str);
90 # return 0 of there's no digits
91 function format_int_0($str) {
92 $str = format_int($str);
99 function format_zip($str) {
100 $str = ereg_replace('[^0-9]', '', $str);
101 if(strlen($str) > 5) {
102 return substr($str, 0, 5) . '-' . substr($str, 5);
107 function format_filename($str, $allow_uppercase = false) {
108 if(!$allow_uppercase) {
109 $str = strtolower($str);
111 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
112 return ereg_replace('^[.-]', '_', $str);
115 function format_path($str, $allow_uppercase = false) {
116 if(!$allow_uppercase) {
117 $str = strtolower($str);
119 $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
120 return ereg_replace('^[.-]', '_', $str);
123 function client_path_to_filename($path) {
124 $filename = ereg_replace(".*[:/\\]", '', $path);
125 return format_filename($filename, true);
129 function format_image_w_h($str) {
130 $fields = explode(' ', $str);
131 if(count($fields) != 3) {
135 list($filename, $width, $height) = $fields;
136 $filename = format_path($filename);
137 $width = format_int_0($width);
138 $height = format_int_0($height);
140 return "$filename $width $height";
143 function format_image_w_h_thumb_w_h($str) {
144 $fields = explode(' ', $str);
145 if(count($fields) != 6) {
146 die('count: ' . count($fields));
150 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
151 $filename = format_path($filename);
152 $width = format_int_0($width);
153 $height = format_int_0($height);
154 $thumb_filename = format_path($thumb_filename);
155 $thumb_width = format_int_0($thumb_width);
156 $thumb_height = format_int_0($thumb_height);
158 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
161 function format_varname($str) {
162 $str = strtolower($str);
163 $str = ereg_replace('[^a-z0-9_]', '_', $str);
164 return ereg_replace('^[0-9]*', '', $str);
167 function format_oneline($str) {
168 $str = str_replace("\r", '', $str);
169 return str_replace("\n", '', $str);
172 function format_unix($str) {
173 return unix_newlines($str);
176 function format_bool($str) {
177 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
184 function format_yesno($str) {
185 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
192 function format_email($str) {
194 return trim(format_oneline($str));
197 function format_url($str) {
198 # FIXME check for TLD? encode special chars?
199 $str = trim(format_oneline($str));
201 if(strpos($str, ':') === false) {
202 $str = 'http://' . $str;
208 function format_money($str, $display_cents = true) {
209 $str = ereg_replace('[^0-9.]', '', $str);
211 $int = (int)($str * 100);
213 $cents = sprintf('.%02d', $cents);
214 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
219 $chars = (string)$int;
222 $index = strlen($chars);
228 $output = ',' . $output;
230 $char = substr($chars, $index, 1);
231 $output = $char . $output;
233 $output = '$' . $output . $cents;
237 function format_dollars($str) {
238 return format_money($str, false);
241 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
242 function format_mdy_to_ymd($str) {
243 require_once('code/wfpl/time.php');
244 return mdy_to_ymd(format_oneline($str));
248 function format_ymd($str) {
249 require_once('code/wfpl/time.php');
250 list($year, $month, $day) = ymd_clean($str);
251 return sprintf('%04u-%02u-%02u', $year, $month, $day);
254 # takes any of: HH :MM HH:MM
255 # returns decimal number of hours
257 # You probably want to use format_hours() instead because it handles hours with a decimal point.
258 function format_hours_minutes($str) {
259 if(strlen($str) == 0) {
262 $pos = strpos($str, ':');
264 $hours = format_int_0($str);
266 } elseif($pos == 0) {
268 $minutes = format_int_0($str);
270 $hours = format_int_0(substr($str, 0, $pos));
271 $minutes = format_int_0(substr($str, $pos + 1));
273 return $hours + ($minutes / 60.0);
276 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
277 # returns decimal number of hours
278 function format_hours($str) {
279 if(strlen($str) == 0) {
282 if(strpos($str, ':') !== false) {
283 return format_hours_minutes($str);
285 return format_decimal($str);
290 # returns decimal number of hours since midnight
291 function format_12hr_to_hours($str) {
292 if(eregi('noon', $str)) {
295 $hours = format_hours($str);
296 if($hours < 12 && eregi('p', $str)) {
303 function format_phone($str) {
304 $str = ereg_replace('[^0-9]', '', $str);
305 $str = ereg_replace('^1*', '', $str);
309 if($len < 10 && $len != 7) {
310 #NOT A VALID PHONE NUMBER
315 $output = ' ext: ' . substr($str, 10);
320 $area = substr($str, 0, 3);
321 $str = substr($str, 3);
324 $output = substr($str, 3) . $output;
325 $output = substr($str, 0, 3) . '-' . $output;
328 $output = "($area) " . $output;