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_positive_decimal($str) {
49 $str = ereg_replace('[^0-9.]', '', $str);
50 $pos = strpos($str, '.');
52 $str = str_replace('.', '', $str);
55 } elseif($pos == strlen($str)) {
58 return substr($str, 0, $pos) . '.' . substr($str, $pos);
64 function format_decimal($str) {
65 $str = ereg_replace('[^0-9.-]', '', $str);
66 if(substr($str, 0, 1) == '-') {
67 $str = format_positive_decimal(substr($str, 1));
68 if($str !== '' && $str !== '0' && $str != '0.0') {
73 return format_positive_decimal($str);
77 # return 0 of there's no digits
78 function format_int_0($str) {
79 $str = format_int($str);
86 function format_zip($str) {
87 $str = ereg_replace('[^0-9]', '', $str);
88 if(strlen($str) > 5) {
89 return substr($str, 0, 5) . '-' . substr($str, 5);
94 function format_filename($str, $allow_uppercase = false) {
95 if(!$allow_uppercase) {
96 $str = strtolower($str);
98 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
99 return ereg_replace('^[.-]', '_', $str);
102 function client_path_to_filename($path) {
103 $filename = ereg_replace(".*[:/\\]", '', $path);
104 return format_filename($filename, true);
108 function format_h_w_image($str) {
109 $fields = explode(' ', $str);
110 if(count($fields) != 3) {
114 list($width, $height, $filename) = $fields;
115 $width = format_int_0($width);
116 $height = format_int_0($height);
117 $filename = format_filename($filename);
119 return "$width $height $filename";
122 function format_varname($str) {
123 $str = strtolower($str);
124 $str = ereg_replace('[^a-z0-9_]', '_', $str);
125 return ereg_replace('^[0-9]*', '', $str);
128 function format_oneline($str) {
129 $str = str_replace("\r", '', $str);
130 return str_replace("\n", '', $str);
133 function format_unix($str) {
134 return unix_newlines($str);
137 function format_bool($str) {
138 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
145 function format_yesno($str) {
146 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
153 function format_email($str) {
155 return trim(format_oneline($str));
158 function format_url($str) {
159 # FIXME check for TLD? encode special chars?
160 $str = trim(format_oneline($str));
162 if(strpos($str, ':') === false) {
163 $str = 'http://' . $str;
169 function format_money($str, $display_cents = true) {
170 $str = ereg_replace('[^0-9.]', '', $str);
172 $int = (int)($str * 100);
174 $cents = sprintf('.%02d', $cents);
175 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
180 $chars = (string)$int;
183 $index = strlen($chars);
189 $output = ',' . $output;
191 $char = substr($chars, $index, 1);
192 $output = $char . $output;
194 $output = '$' . $output . $cents;
198 function format_dollars($str) {
199 return format_money($str, false);
202 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
203 function format_mdy_to_ymd($str) {
204 require_once('code/wfpl/time.php');
205 return mdy_to_ymd(format_oneline($str));
208 function format_phone($str) {
209 $str = ereg_replace('[^0-9]', '', $str);
210 $str = ereg_replace('^1*', '', $str);
214 if($len < 10 && $len != 7) {
215 #NOT A VALID PHONE NUMBER
220 $output = ' ext: ' . substr($str, 10);
225 $area = substr($str, 0, 3);
226 $str = substr($str, 3);
229 $output = substr($str, 3) . $output;
230 $output = substr($str, 0, 3) . '-' . $output;
233 $output = "($area) " . $output;