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 = preg_replace("/([a-z])([A-Z])/", "\\1 \\2", $str); # split words on camelCase
23 $str = str_replace('_', ' ', $str);
28 # This function makes sure that $str is in the list of options, and returns "" otherwise
29 function format_options($str, $name) {
30 if(!isset($GLOBALS[$name . '_options'])) {
31 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
34 foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
35 list($key, $value) = $keyval;
44 function format_int($str) {
45 $str = ereg_replace('[^0-9]', '', $str);
46 return ereg_replace('^0*([0-9])', '\1', $str);
49 # format the digits after the decimal point
50 function format_decimals($str) {
51 $str = ereg_replace('[^0-9]', '', $str);
53 $str = substr($str, 0, 1) . ereg_replace('0*$', '', substr($str, 1));
58 function _format_positive_decimal($str) {
59 $str = ereg_replace('[^0-9.]', '', $str);
60 $pos = strpos($str, '.');
62 $str = str_replace('.', '', $str);
64 return '0.' . format_decimals($str);
65 } elseif($pos == strlen($str)) {
66 return format_int($str);
68 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
71 return format_int($str);
74 function format_positive_decimal($str) {
75 $str = _format_positive_decimal($str);
76 if(substr($str, -2) === '.0') {
77 $str = substr($str, 0, -2);
82 function format_decimal($str) {
83 $str = ereg_replace('[^0-9.-]', '', $str);
84 if(substr($str, 0, 1) == '-') {
85 $str = format_positive_decimal(substr($str, 1));
86 if($str !== '' && $str !== '0') {
91 return format_positive_decimal($str);
95 # return 0 of there's no digits
96 function format_int_0($str) {
97 $str = format_int($str);
105 function format_zip($str) {
106 $str = ereg_replace('[^0-9]', '', $str);
107 if(strlen($str) > 5) {
108 return substr($str, 0, 5) . '-' . substr($str, 5);
113 function format_filename($str, $allow_uppercase = false) {
114 if(!$allow_uppercase) {
115 $str = strtolower($str);
117 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
118 return ereg_replace('^[.-]', '_', $str);
121 function format_path($str, $allow_uppercase = false) {
122 if(!$allow_uppercase) {
123 $str = strtolower($str);
125 $str = ereg_replace('[^a-zA-Z0-9_./-]', '_', $str);
126 return ereg_replace('^[.-]', '_', $str);
129 function client_path_to_filename($path) {
130 $filename = ereg_replace(".*[:/\\]", '', $path);
131 return format_filename($filename, true);
135 function format_image_w_h($str) {
136 $fields = explode(' ', $str);
137 if(count($fields) != 3) {
141 list($filename, $width, $height) = $fields;
142 $filename = format_path($filename);
143 $width = format_int_0($width);
144 $height = format_int_0($height);
146 return "$filename $width $height";
149 function format_image_w_h_thumb_w_h($str) {
150 $fields = explode(' ', $str);
151 if(count($fields) != 6) {
155 list($filename, $width, $height, $thumb_filename, $thumb_width, $thumb_height) = $fields;
156 $filename = format_path($filename);
157 $width = format_int_0($width);
158 $height = format_int_0($height);
159 $thumb_filename = format_path($thumb_filename);
160 $thumb_width = format_int_0($thumb_width);
161 $thumb_height = format_int_0($thumb_height);
163 return "$filename $width $height $thumb_filename $thumb_width $thumb_height";
166 function format_varname($str) {
167 $str = preg_replace("/([a-z])([A-Z])/", "\\1_\\2", $str); # split words on camelCase
168 $str = strtolower($str);
169 $str = ereg_replace('[^a-z0-9_]', '_', $str);
170 return ereg_replace('^[0-9]*', '', $str);
173 function format_oneline($str) {
174 $str = str_replace("\r", '', $str);
175 return str_replace("\n", '', $str);
178 function format_unix($str) {
179 return unix_newlines($str);
182 function format_bool($str) {
183 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
190 function format_yesno($str) {
191 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
198 function format_email($str) {
200 return trim(format_oneline($str));
203 function format_url($str) {
204 # FIXME check for TLD? encode special chars?
205 $str = trim(format_oneline($str));
207 if(strpos($str, ':') === false) {
208 $str = 'http://' . $str;
214 # pass a string containing only numeric digits.
215 # returns a string containing only numeric digits which is 1 greater.
216 function strplusone($str) {
218 $a = str_split($str);
221 $digit = array_pop($a);
237 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
238 # parameter) and prints like so: $12,456.79 or $12,457
239 function format_money($str, $display_cents = true) {
240 $str = format_decimal($str);
244 if(strpos($str, '.')) {
245 list($int, $decimals) = explode('.', $str);
246 if(strlen($decimals) == 1) {
250 if(strlen($decimals) > 2) {
251 # round up to the nearest penny
252 if(substr($decimals, 2, 1) >= 5) {
253 $decimals = strplusone(substr($decimals, 0, 2));
254 if($decimals == '100') {
256 $int = strplusone($int);
259 $decimals = substr($decimals, 0, 2);
262 $cents = ".$decimals";
264 if(substr($decimals, 0, 1) >= 5) {
265 $int = strplusone($int);
274 $chars = str_split($int);
281 $output = ',' . $output;
283 $char = array_pop($chars);
284 $output = $char . $output;
286 $output = '$' . $output . $cents;
291 function format_dollars($str) {
292 return format_money($str, false);
295 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
296 function format_mdy_to_ymd($str) {
297 if($str == '') return '';
298 require_once(__DIR__ . '/time.php');
299 return mdy_to_ymd(format_oneline($str));
303 function format_ymd($str) {
304 require_once(__DIR__ . '/time.php');
305 list($year, $month, $day) = ymd_clean($str);
306 return sprintf('%04u-%02u-%02u', $year, $month, $day);
309 # takes any of: HH :MM HH:MM
310 # returns decimal number of hours
312 # You probably want to use format_hours() instead because it handles hours with a decimal point.
313 function format_hours_minutes($str) {
314 if(strlen($str) == 0) {
317 $pos = strpos($str, ':');
319 $hours = format_int_0($str);
321 } elseif($pos == 0) {
323 $minutes = format_int_0($str);
325 $hours = format_int_0(substr($str, 0, $pos));
326 $minutes = format_int_0(substr($str, $pos + 1));
328 return $hours + ($minutes / 60.0);
331 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
332 # returns decimal number of hours
333 function format_hours($str) {
334 if(strlen($str) == 0) {
337 if(strpos($str, ':') !== false) {
338 return format_hours_minutes($str);
340 return format_decimal($str);
345 # returns decimal number of hours since midnight
346 function format_12hr_to_hours($str) {
347 if(eregi('noon', $str)) {
350 $hours = format_hours($str);
351 if($hours < 12 && eregi('p', $str)) {
358 function format_phone($str) {
359 $str = ereg_replace('[^0-9]', '', $str);
360 $str = ereg_replace('^1*', '', $str);
364 if($len < 10 && $len != 7) {
365 #NOT A VALID PHONE NUMBER
370 $output = ' ext: ' . substr($str, 10);
375 $area = substr($str, 0, 3);
376 $str = substr($str, 3);
379 $output = substr($str, 3) . $output;
380 $output = substr($str, 0, 3) . '-' . $output;
383 $output = "($area) " . $output;