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 # format the digits after the decimal point
49 function format_decimals($str) {
50 $str = ereg_replace('[^0-9]', '', $str);
52 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
57 function _format_positive_decimal($str) {
58 $str = ereg_replace('[^0-9.]', '', $str);
59 $pos = strpos($str, '.');
61 $str = str_replace('.', '', $str);
63 return '0.' . format_decimals($str);
64 } elseif($pos == strlen($str)) {
65 return format_int($str);
67 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
70 return format_int($str);
73 function format_positive_decimal($str) {
74 $str = _format_positive_decimal($str);
81 function format_decimal($str) {
82 $str = ereg_replace('[^0-9.-]', '', $str);
83 if(substr($str, 0, 1) == '-') {
84 $str = format_positive_decimal(substr($str, 1));
85 if($str !== '' && $str !== '0') {
90 return format_positive_decimal($str);
94 # return 0 of there's no digits
95 function format_int_0($str) {
96 $str = format_int($str);
104 function format_zip($str) {
105 $str = ereg_replace('[^0-9]', '', $str);
106 if(strlen($str) > 5) {
107 return substr($str, 0, 5) . '-' . substr($str, 5);
112 function format_filename($str, $allow_uppercase = false) {
113 if(!$allow_uppercase) {
114 $str = strtolower($str);
116 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
117 return ereg_replace('^[.-]', '_', $str);
120 function format_path($str, $allow_uppercase = false) {
121 if(!$allow_uppercase) {
122 $str = strtolower($str);
124 $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
125 return ereg_replace('^[.-]', '_', $str);
128 function client_path_to_filename($path) {
129 $filename = ereg_replace(".*[:/\\]", '', $path);
130 return format_filename($filename, true);
134 function format_image_w_h($str) {
135 $fields = explode(' ', $str);
136 if(count($fields) != 3) {
140 list($filename, $width, $height) = $fields;
141 $filename = format_path($filename);
142 $width = format_int_0($width);
143 $height = format_int_0($height);
145 return "$filename $width $height";
148 function format_image_w_h_thumb_w_h($str) {
149 $fields = explode(' ', $str);
150 if(count($fields) != 6) {
154 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
155 $filename = format_path($filename);
156 $width = format_int_0($width);
157 $height = format_int_0($height);
158 $thumb_filename = format_path($thumb_filename);
159 $thumb_width = format_int_0($thumb_width);
160 $thumb_height = format_int_0($thumb_height);
162 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
165 function format_varname($str) {
166 $str = strtolower($str);
167 $str = ereg_replace('[^a-z0-9_]', '_', $str);
168 return ereg_replace('^[0-9]*', '', $str);
171 function format_oneline($str) {
172 $str = str_replace("\r", '', $str);
173 return str_replace("\n", '', $str);
176 function format_unix($str) {
177 return unix_newlines($str);
180 function format_bool($str) {
181 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
188 function format_yesno($str) {
189 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
196 function format_email($str) {
198 return trim(format_oneline($str));
201 function format_url($str) {
202 # FIXME check for TLD? encode special chars?
203 $str = trim(format_oneline($str));
205 if(strpos($str, ':') === false) {
206 $str = 'http://' . $str;
212 # pass a string containing only numeric digits.
213 # returns a string containing only numeric digits which is 1 greater.
214 function strplusone($str) {
216 $a = str_split($str);
219 $digit = array_pop($a);
235 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
236 # parameter) and prints like so: $12,456.79 or $12,457
237 function format_money($str, $display_cents = true) {
238 $str = format_decimal($str);
242 if(strpos($str, '.')) {
243 list($int, $decimals) = explode('.', $str);
244 if(strlen($decimals) == 1) {
248 if(strlen($decimals) > 2) {
249 # round up to the nearest penny
250 if(substr($decimals, 2, 1) >= 5) {
251 $decimals = strplusone(substr($decimals, 0, 2));
252 if($decimals == '100') {
254 $int = strplusone($int);
257 $decimals = substr($decimals, 0, 2);
260 $cents = ".$decimals";
262 if(substr($decimals, 0, 1) >= 5) {
263 $int = strplusone($int);
272 $chars = str_split($int);
279 $output = ',' . $output;
281 $char = array_pop($chars);
282 $output = $char . $output;
284 $output = '$' . $output . $cents;
289 function format_dollars($str) {
290 return format_money($str, false);
293 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
294 function format_mdy_to_ymd($str) {
295 require_once('code/wfpl/time.php');
296 return mdy_to_ymd(format_oneline($str));
300 function format_ymd($str) {
301 require_once('code/wfpl/time.php');
302 list($year, $month, $day) = ymd_clean($str);
303 return sprintf('%04u-%02u-%02u', $year, $month, $day);
306 # takes any of: HH :MM HH:MM
307 # returns decimal number of hours
309 # You probably want to use format_hours() instead because it handles hours with a decimal point.
310 function format_hours_minutes($str) {
311 if(strlen($str) == 0) {
314 $pos = strpos($str, ':');
316 $hours = format_int_0($str);
318 } elseif($pos == 0) {
320 $minutes = format_int_0($str);
322 $hours = format_int_0(substr($str, 0, $pos));
323 $minutes = format_int_0(substr($str, $pos + 1));
325 return $hours + ($minutes / 60.0);
328 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
329 # returns decimal number of hours
330 function format_hours($str) {
331 if(strlen($str) == 0) {
334 if(strpos($str, ':') !== false) {
335 return format_hours_minutes($str);
337 return format_decimal($str);
342 # returns decimal number of hours since midnight
343 function format_12hr_to_hours($str) {
344 if(eregi('noon', $str)) {
347 $hours = format_hours($str);
348 if($hours < 12 && eregi('p', $str)) {
355 function format_phone($str) {
356 $str = ereg_replace('[^0-9]', '', $str);
357 $str = ereg_replace('^1*', '', $str);
361 if($len < 10 && $len != 7) {
362 #NOT A VALID PHONE NUMBER
367 $output = ' ext: ' . substr($str, 10);
372 $area = substr($str, 0, 3);
373 $str = substr($str, 3);
376 $output = substr($str, 3) . $output;
377 $output = substr($str, 0, 3) . '-' . $output;
380 $output = "($area) " . $output;