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);
51 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
56 function _format_positive_decimal($str) {
57 $str = ereg_replace('[^0-9.]', '', $str);
58 $pos = strpos($str, '.');
60 $str = str_replace('.', '', $str);
62 return '0.' . format_decimals($str);
63 } elseif($pos == strlen($str)) {
64 return format_int($str);
66 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
69 return format_int($str);
72 function format_positive_decimal($str) {
73 $str = _format_positive_decimal($str);
80 function format_decimal($str) {
81 $str = ereg_replace('[^0-9.-]', '', $str);
82 if(substr($str, 0, 1) == '-') {
83 $str = format_positive_decimal(substr($str, 1));
84 if($str !== '' && $str !== '0') {
89 return format_positive_decimal($str);
93 # return 0 of there's no digits
94 function format_int_0($str) {
95 $str = format_int($str);
102 function format_zip($str) {
103 $str = ereg_replace('[^0-9]', '', $str);
104 if(strlen($str) > 5) {
105 return substr($str, 0, 5) . '-' . substr($str, 5);
110 function format_filename($str, $allow_uppercase = false) {
111 if(!$allow_uppercase) {
112 $str = strtolower($str);
114 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
115 return ereg_replace('^[.-]', '_', $str);
118 function format_path($str, $allow_uppercase = false) {
119 if(!$allow_uppercase) {
120 $str = strtolower($str);
122 $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
123 return ereg_replace('^[.-]', '_', $str);
126 function client_path_to_filename($path) {
127 $filename = ereg_replace(".*[:/\\]", '', $path);
128 return format_filename($filename, true);
132 function format_image_w_h($str) {
133 $fields = explode(' ', $str);
134 if(count($fields) != 3) {
138 list($filename, $width, $height) = $fields;
139 $filename = format_path($filename);
140 $width = format_int_0($width);
141 $height = format_int_0($height);
143 return "$filename $width $height";
146 function format_image_w_h_thumb_w_h($str) {
147 $fields = explode(' ', $str);
148 if(count($fields) != 6) {
149 die('count: ' . count($fields));
153 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
154 $filename = format_path($filename);
155 $width = format_int_0($width);
156 $height = format_int_0($height);
157 $thumb_filename = format_path($thumb_filename);
158 $thumb_width = format_int_0($thumb_width);
159 $thumb_height = format_int_0($thumb_height);
161 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
164 function format_varname($str) {
165 $str = strtolower($str);
166 $str = ereg_replace('[^a-z0-9_]', '_', $str);
167 return ereg_replace('^[0-9]*', '', $str);
170 function format_oneline($str) {
171 $str = str_replace("\r", '', $str);
172 return str_replace("\n", '', $str);
175 function format_unix($str) {
176 return unix_newlines($str);
179 function format_bool($str) {
180 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
187 function format_yesno($str) {
188 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
195 function format_email($str) {
197 return trim(format_oneline($str));
200 function format_url($str) {
201 # FIXME check for TLD? encode special chars?
202 $str = trim(format_oneline($str));
204 if(strpos($str, ':') === false) {
205 $str = 'http://' . $str;
211 # pass a string containing only numeric digits.
212 # returns a string containing only numeric digits which is 1 greater.
213 function strplusone($str) {
215 $a = str_split($str);
218 $digit = array_pop($a);
234 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
235 # parameter) and prints like so: $12,456.79 or $12,457
236 function format_money($str, $display_cents = true) {
237 $str = format_decimal($str);
241 if(strpos($str, '.')) {
242 list($int, $decimals) = explode('.', $str);
243 if(strlen($decimals) == 1) {
247 if(strlen($decimals) > 2) {
248 # round up to the nearest penny
249 if(substr($decimals, 2, 1) >= 5) {
250 $decimals = strplusone(substr($decimals, 0, 2));
251 if($decimals == '100') {
253 $int = strplusone($int);
256 $decimals = substr($decimals, 0, 2);
259 $cents = ".$decimals";
261 if(substr($decimals, 0, 1) >= 5) {
262 $int = strplusone($int);
271 $chars = str_split($int);
278 $output = ',' . $output;
280 $char = array_pop($chars);
281 $output = $char . $output;
283 $output = '$' . $output . $cents;
288 function format_dollars($str) {
289 return format_money($str, false);
292 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
293 function format_mdy_to_ymd($str) {
294 require_once('code/wfpl/time.php');
295 return mdy_to_ymd(format_oneline($str));
299 function format_ymd($str) {
300 require_once('code/wfpl/time.php');
301 list($year, $month, $day) = ymd_clean($str);
302 return sprintf('%04u-%02u-%02u', $year, $month, $day);
305 # takes any of: HH :MM HH:MM
306 # returns decimal number of hours
308 # You probably want to use format_hours() instead because it handles hours with a decimal point.
309 function format_hours_minutes($str) {
310 if(strlen($str) == 0) {
313 $pos = strpos($str, ':');
315 $hours = format_int_0($str);
317 } elseif($pos == 0) {
319 $minutes = format_int_0($str);
321 $hours = format_int_0(substr($str, 0, $pos));
322 $minutes = format_int_0(substr($str, $pos + 1));
324 return $hours + ($minutes / 60.0);
327 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
328 # returns decimal number of hours
329 function format_hours($str) {
330 if(strlen($str) == 0) {
333 if(strpos($str, ':') !== false) {
334 return format_hours_minutes($str);
336 return format_decimal($str);
341 # returns decimal number of hours since midnight
342 function format_12hr_to_hours($str) {
343 if(eregi('noon', $str)) {
346 $hours = format_hours($str);
347 if($hours < 12 && eregi('p', $str)) {
354 function format_phone($str) {
355 $str = ereg_replace('[^0-9]', '', $str);
356 $str = ereg_replace('^1*', '', $str);
360 if($len < 10 && $len != 7) {
361 #NOT A VALID PHONE NUMBER
366 $output = ' ext: ' . substr($str, 10);
371 $area = substr($str, 0, 3);
372 $str = substr($str, 3);
375 $output = substr($str, 3) . $output;
376 $output = substr($str, 0, 3) . '-' . $output;
379 $output = "($area) " . $output;