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_slug($str) {
167 $str = strtolower($str);
168 $str = str_replace("'", '', $str);
169 $str = preg_replace('/[^a-z0-9-]+/', '-', $str);
170 return trim($str, '-');
173 function format_varname($str) {
174 $str = preg_replace("/([a-z])([A-Z])/", "\\1_\\2", $str); # split words on camelCase
175 $str = strtolower($str);
176 $str = preg_replace('/[^a-z0-9_]+/', '_', $str);
177 return preg_replace('/^([^a-z_])/', "_\\1", $str);
180 function format_oneline($str) {
181 $str = str_replace("\r", '', $str);
182 return str_replace("\n", '', $str);
185 function format_unix($str) {
186 return unix_newlines($str);
189 function format_bool($str) {
190 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
197 function format_yesno($str) {
198 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
205 function format_email($str) {
207 return trim(format_oneline($str));
210 function format_url($str) {
211 # FIXME check for TLD? encode special chars?
212 $str = trim(format_oneline($str));
214 if(strpos($str, ':') === false) {
215 $str = 'http://' . $str;
221 # pass a string containing only numeric digits.
222 # returns a string containing only numeric digits which is 1 greater.
223 function strplusone($str) {
225 $a = str_split($str);
228 $digit = array_pop($a);
244 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
245 # parameter) and prints like so: $12,456.79 or $12,457
246 function format_money($str, $display_cents = true) {
247 $str = format_decimal($str);
251 if(strpos($str, '.')) {
252 list($int, $decimals) = explode('.', $str);
253 if(strlen($decimals) == 1) {
257 if(strlen($decimals) > 2) {
258 # round up to the nearest penny
259 if(substr($decimals, 2, 1) >= 5) {
260 $decimals = strplusone(substr($decimals, 0, 2));
261 if($decimals == '100') {
263 $int = strplusone($int);
266 $decimals = substr($decimals, 0, 2);
269 $cents = ".$decimals";
271 if(substr($decimals, 0, 1) >= 5) {
272 $int = strplusone($int);
281 $chars = str_split($int);
288 $output = ',' . $output;
290 $char = array_pop($chars);
291 $output = $char . $output;
293 $output = '$' . $output . $cents;
298 function format_dollars($str) {
299 return format_money($str, false);
302 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
303 function format_mdy_to_ymd($str) {
304 if($str == '') return '';
305 require_once(__DIR__.'/'.'time.php');
306 return mdy_to_ymd(format_oneline($str));
310 function format_ymd($str) {
311 require_once(__DIR__.'/'.'time.php');
312 list($year, $month, $day) = ymd_clean($str);
313 return sprintf('%04u-%02u-%02u', $year, $month, $day);
316 # takes any of: HH :MM HH:MM
317 # returns decimal number of hours
319 # You probably want to use format_hours() instead because it handles hours with a decimal point.
320 function format_hours_minutes($str) {
321 if(strlen($str) == 0) {
324 $pos = strpos($str, ':');
326 $hours = format_int_0($str);
328 } elseif($pos == 0) {
330 $minutes = format_int_0($str);
332 $hours = format_int_0(substr($str, 0, $pos));
333 $minutes = format_int_0(substr($str, $pos + 1));
335 return $hours + ($minutes / 60.0);
338 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
339 # returns decimal number of hours
340 function format_hours($str) {
341 if(strlen($str) == 0) {
344 if(strpos($str, ':') !== false) {
345 return format_hours_minutes($str);
347 return format_decimal($str);
352 # returns decimal number of hours since midnight
353 function format_12hr_to_hours($str) {
354 if(eregi('noon', $str)) {
357 $hours = format_hours($str);
358 if($hours < 12 && eregi('p', $str)) {
365 function format_phone($str) {
366 $str = ereg_replace('[^0-9]', '', $str);
367 $str = ereg_replace('^1*', '', $str);
371 if($len < 10 && $len != 7) {
372 #NOT A VALID PHONE NUMBER
377 $output = ' ext: ' . substr($str, 10);
382 $area = substr($str, 0, 3);
383 $str = substr($str, 3);
386 $output = substr($str, 3) . $output;
387 $output = substr($str, 0, 3) . '-' . $output;
390 $output = "($area) " . $output;