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("/['‘’]/", '', $str);
177 $str = preg_replace('/[^a-z0-9_]+/', '_', $str);
178 return preg_replace('/^([^a-z_])/', "_\\1", $str);
181 function format_oneline($str) {
182 $str = str_replace("\r", '', $str);
183 return str_replace("\n", '', $str);
186 function format_unix($str) {
187 return unix_newlines($str);
190 function format_bool($str) {
191 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
198 function format_yesno($str) {
199 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false' && $str !== 'no' && $str !== 'N' && $str !== 'n') {
206 function format_email($str) {
208 return trim(format_oneline($str));
211 function format_url($str) {
212 # FIXME check for TLD? encode special chars?
213 $str = trim(format_oneline($str));
215 if(strpos($str, ':') === false) {
216 $str = 'http://' . $str;
222 # pass a string containing only numeric digits.
223 # returns a string containing only numeric digits which is 1 greater.
224 function strplusone($str) {
226 $a = str_split($str);
229 $digit = array_pop($a);
245 # rounds properly to the nearest penny (or dollar if you pass false for the 2nd
246 # parameter) and prints like so: $12,456.79 or $12,457
247 function format_money($str, $display_cents = true) {
248 $str = format_decimal($str);
252 if(strpos($str, '.')) {
253 list($int, $decimals) = explode('.', $str);
254 if(strlen($decimals) == 1) {
258 if(strlen($decimals) > 2) {
259 # round up to the nearest penny
260 if(substr($decimals, 2, 1) >= 5) {
261 $decimals = strplusone(substr($decimals, 0, 2));
262 if($decimals == '100') {
264 $int = strplusone($int);
267 $decimals = substr($decimals, 0, 2);
270 $cents = ".$decimals";
272 if(substr($decimals, 0, 1) >= 5) {
273 $int = strplusone($int);
282 $chars = str_split($int);
289 $output = ',' . $output;
291 $char = array_pop($chars);
292 $output = $char . $output;
294 $output = '$' . $output . $cents;
299 function format_dollars($str) {
300 return format_money($str, false);
303 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
304 function format_mdy_to_ymd($str) {
305 if($str == '') return '';
306 require_once(__DIR__.'/'.'time.php');
307 return mdy_to_ymd(format_oneline($str));
311 function format_ymd($str) {
312 require_once(__DIR__.'/'.'time.php');
313 list($year, $month, $day) = ymd_clean($str);
314 return sprintf('%04u-%02u-%02u', $year, $month, $day);
317 # takes any of: HH :MM HH:MM
318 # returns decimal number of hours
320 # You probably want to use format_hours() instead because it handles hours with a decimal point.
321 function format_hours_minutes($str) {
322 if(strlen($str) == 0) {
325 $pos = strpos($str, ':');
327 $hours = format_int_0($str);
329 } elseif($pos == 0) {
331 $minutes = format_int_0($str);
333 $hours = format_int_0(substr($str, 0, $pos));
334 $minutes = format_int_0(substr($str, $pos + 1));
336 return $hours + ($minutes / 60.0);
339 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
340 # returns decimal number of hours
341 function format_hours($str) {
342 if(strlen($str) == 0) {
345 if(strpos($str, ':') !== false) {
346 return format_hours_minutes($str);
348 return format_decimal($str);
353 # returns decimal number of hours since midnight
354 function format_12hr_to_hours($str) {
355 if(eregi('noon', $str)) {
358 $hours = format_hours($str);
359 if($hours < 12 && eregi('p', $str)) {
366 function format_phone($str) {
367 $str = ereg_replace('[^0-9]', '', $str);
368 $str = ereg_replace('^1*', '', $str);
372 if($len < 10 && $len != 7) {
373 #NOT A VALID PHONE NUMBER
378 $output = ' ext: ' . substr($str, 10);
383 $area = substr($str, 0, 3);
384 $str = substr($str, 3);
387 $output = substr($str, 3) . $output;
388 $output = substr($str, 0, 3) . '-' . $output;
391 $output = "($area) " . $output;