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) {
152 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
153 $filename = format_path($filename);
154 $width = format_int_0($width);
155 $height = format_int_0($height);
156 $thumb_filename = format_path($thumb_filename);
157 $thumb_width = format_int_0($thumb_width);
158 $thumb_height = format_int_0($thumb_height);
160 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
163 function format_varname($str) {
164 $str = strtolower($str);
165 $str = ereg_replace('[^a-z0-9_]', '_', $str);
166 return ereg_replace('^[0-9]*', '', $str);
169 function format_oneline($str) {
170 $str = str_replace("\r", '', $str);
171 return str_replace("\n", '', $str);
174 function format_unix($str) {
175 return unix_newlines($str);
178 function format_bool($str) {
179 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
186 function format_yesno($str) {
187 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
194 function format_email($str) {
196 return trim(format_oneline($str));
199 function format_url($str) {
200 # FIXME check for TLD? encode special chars?
201 $str = trim(format_oneline($str));
203 if(strpos($str, ':') === false) {
204 $str = 'http://' . $str;
210 # pass a string containing only numeric digits.
211 # returns a string containing only numeric digits which is 1 greater.
212 function strplusone($str) {
214 $a = str_split($str);
217 $digit = array_pop($a);
233 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
234 # parameter) and prints like so: $12,456.79 or $12,457
235 function format_money($str, $display_cents = true) {
236 $str = format_decimal($str);
240 if(strpos($str, '.')) {
241 list($int, $decimals) = explode('.', $str);
242 if(strlen($decimals) == 1) {
246 if(strlen($decimals) > 2) {
247 # round up to the nearest penny
248 if(substr($decimals, 2, 1) >= 5) {
249 $decimals = strplusone(substr($decimals, 0, 2));
250 if($decimals == '100') {
252 $int = strplusone($int);
255 $decimals = substr($decimals, 0, 2);
258 $cents = ".$decimals";
260 if(substr($decimals, 0, 1) >= 5) {
261 $int = strplusone($int);
270 $chars = str_split($int);
277 $output = ',' . $output;
279 $char = array_pop($chars);
280 $output = $char . $output;
282 $output = '$' . $output . $cents;
287 function format_dollars($str) {
288 return format_money($str, false);
291 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
292 function format_mdy_to_ymd($str) {
293 require_once('code/wfpl/time.php');
294 return mdy_to_ymd(format_oneline($str));
298 function format_ymd($str) {
299 require_once('code/wfpl/time.php');
300 list($year, $month, $day) = ymd_clean($str);
301 return sprintf('%04u-%02u-%02u', $year, $month, $day);
304 # takes any of: HH :MM HH:MM
305 # returns decimal number of hours
307 # You probably want to use format_hours() instead because it handles hours with a decimal point.
308 function format_hours_minutes($str) {
309 if(strlen($str) == 0) {
312 $pos = strpos($str, ':');
314 $hours = format_int_0($str);
316 } elseif($pos == 0) {
318 $minutes = format_int_0($str);
320 $hours = format_int_0(substr($str, 0, $pos));
321 $minutes = format_int_0(substr($str, $pos + 1));
323 return $hours + ($minutes / 60.0);
326 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
327 # returns decimal number of hours
328 function format_hours($str) {
329 if(strlen($str) == 0) {
332 if(strpos($str, ':') !== false) {
333 return format_hours_minutes($str);
335 return format_decimal($str);
340 # returns decimal number of hours since midnight
341 function format_12hr_to_hours($str) {
342 if(eregi('noon', $str)) {
345 $hours = format_hours($str);
346 if($hours < 12 && eregi('p', $str)) {
353 function format_phone($str) {
354 $str = ereg_replace('[^0-9]', '', $str);
355 $str = ereg_replace('^1*', '', $str);
359 if($len < 10 && $len != 7) {
360 #NOT A VALID PHONE NUMBER
365 $output = ' ext: ' . substr($str, 10);
370 $area = substr($str, 0, 3);
371 $str = substr($str, 3);
374 $output = substr($str, 3) . $output;
375 $output = substr($str, 0, 3) . '-' . $output;
378 $output = "($area) " . $output;