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);
50 return ereg_replace('^([0-9])0*', '\1', $str);
53 function _format_positive_decimal($str) {
54 $str = ereg_replace('[^0-9.]', '', $str);
55 $pos = strpos($str, '.');
57 $str = str_replace('.', '', $str);
59 return '0.' . format_decimals($str);
60 } elseif($pos == strlen($str)) {
61 return format_int($str);
63 return format_int(substr($str, 0, $pos)) . '.' . format_decimals(substr($str, $pos));
66 return format_int($str);
69 function format_positive_decimal($str) {
70 $str = _format_positive_decimal($str);
77 function format_decimal($str) {
78 $str = ereg_replace('[^0-9.-]', '', $str);
79 if(substr($str, 0, 1) == '-') {
80 $str = format_positive_decimal(substr($str, 1));
81 if($str !== '' && $str !== '0') {
86 return format_positive_decimal($str);
90 # return 0 of there's no digits
91 function format_int_0($str) {
92 $str = format_int($str);
99 function format_zip($str) {
100 $str = ereg_replace('[^0-9]', '', $str);
101 if(strlen($str) > 5) {
102 return substr($str, 0, 5) . '-' . substr($str, 5);
107 function format_filename($str, $allow_uppercase = false) {
108 if(!$allow_uppercase) {
109 $str = strtolower($str);
111 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
112 return ereg_replace('^[.-]', '_', $str);
115 function client_path_to_filename($path) {
116 $filename = ereg_replace(".*[:/\\]", '', $path);
117 return format_filename($filename, true);
121 function format_h_w_image($str) {
122 $fields = explode(' ', $str);
123 if(count($fields) != 3) {
127 list($width, $height, $filename) = $fields;
128 $width = format_int_0($width);
129 $height = format_int_0($height);
130 $filename = format_filename($filename);
132 return "$width $height $filename";
135 function format_varname($str) {
136 $str = strtolower($str);
137 $str = ereg_replace('[^a-z0-9_]', '_', $str);
138 return ereg_replace('^[0-9]*', '', $str);
141 function format_oneline($str) {
142 $str = str_replace("\r", '', $str);
143 return str_replace("\n", '', $str);
146 function format_unix($str) {
147 return unix_newlines($str);
150 function format_bool($str) {
151 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
158 function format_yesno($str) {
159 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
166 function format_email($str) {
168 return trim(format_oneline($str));
171 function format_url($str) {
172 # FIXME check for TLD? encode special chars?
173 $str = trim(format_oneline($str));
175 if(strpos($str, ':') === false) {
176 $str = 'http://' . $str;
182 function format_money($str, $display_cents = true) {
183 $str = ereg_replace('[^0-9.]', '', $str);
185 $int = (int)($str * 100);
187 $cents = sprintf('.%02d', $cents);
188 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
193 $chars = (string)$int;
196 $index = strlen($chars);
202 $output = ',' . $output;
204 $char = substr($chars, $index, 1);
205 $output = $char . $output;
207 $output = '$' . $output . $cents;
211 function format_dollars($str) {
212 return format_money($str, false);
215 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
216 function format_mdy_to_ymd($str) {
217 require_once('code/wfpl/time.php');
218 return mdy_to_ymd(format_oneline($str));
222 function format_ymd($str) {
223 require_once('code/wfpl/time.php');
224 list($year, $month, $day) = ymd_clean($str);
225 return sprintf('%04u-%02u-%02u', $year, $month, $day);
228 # takes any of: HH :MM HH:MM
229 # returns decimal number of hours
231 # You probably want to use format_hours() instead because it handles hours with a decimal point.
232 function format_hours_minutes($str) {
233 if(strlen($str) == 0) {
236 $pos = strpos($str, ':');
238 $hours = format_int_0($str);
240 } elseif($pos == 0) {
242 $minutes = format_int_0($str);
244 $hours = format_int_0(substr($str, 0, $pos));
245 $minutes = format_int_0(substr($str, $pos + 1));
247 return $hours + ($minutes / 60.0);
250 # takes any of: HH :MM HH:MM HH.hh(decimal hours)
251 # returns decimal number of hours
252 function format_hours($str) {
253 if(strlen($str) == 0) {
256 if(strpos($str, ':') !== false) {
257 return format_hours_minutes($str);
259 return format_decimal($str);
264 # returns decimal number of hours since midnight
265 function format_12hr_to_hours($str) {
266 if(eregi('noon', $str)) {
269 $hours = format_hours($str);
270 if($hours < 12 && eregi('p', $str)) {
277 function format_phone($str) {
278 $str = ereg_replace('[^0-9]', '', $str);
279 $str = ereg_replace('^1*', '', $str);
283 if($len < 10 && $len != 7) {
284 #NOT A VALID PHONE NUMBER
289 $output = ' ext: ' . substr($str, 10);
294 $area = substr($str, 0, 3);
295 $str = substr($str, 3);
298 $output = substr($str, 3) . $output;
299 $output = substr($str, 0, 3) . '-' . $output;
302 $output = "($area) " . $output;