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_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 # return 0 of there's no digits
65 function format_int_0($str) {
66 $str = format_int($str);
73 function format_zip($str) {
74 $str = ereg_replace('[^0-9]', '', $str);
75 if(strlen($str) > 5) {
76 return substr($str, 0, 5) . '-' . substr($str, 5);
81 function format_filename($str, $allow_uppercase = false) {
82 if(!$allow_uppercase) {
83 $str = strtolower($str);
85 $str = ereg_replace('[^a-zA-Z0-9_.-]', '_', $str);
86 return ereg_replace('^[.-]', '_', $str);
89 function client_path_to_filename($path) {
90 $filename = ereg_replace(".*[:/\\]", '', $path);
91 return format_filename($filename, true);
95 function format_h_w_image($str) {
96 $fields = explode(' ', $str);
97 if(count($fields) != 3) {
101 list($width, $height, $filename) = $fields;
102 $width = format_int_0($width);
103 $height = format_int_0($height);
104 $filename = format_filename($filename);
106 return "$width $height $filename";
109 function format_varname($str) {
110 $str = strtolower($str);
111 $str = ereg_replace('[^a-z0-9_]', '_', $str);
112 return ereg_replace('^[0-9]*', '', $str);
115 function format_oneline($str) {
116 $str = str_replace("\r", '', $str);
117 return str_replace("\n", '', $str);
120 function format_unix($str) {
121 return unix_newlines($str);
124 function format_bool($str) {
125 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
132 function format_yesno($str) {
133 if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
140 function format_email($str) {
142 return trim(format_oneline($str));
145 function format_url($str) {
146 # FIXME check for TLD? encode special chars?
147 $str = trim(format_oneline($str));
149 if(strpos($str, ':') === false) {
150 $str = 'http://' . $str;
156 function format_money($str, $display_cents = true) {
157 $str = ereg_replace('[^0-9.]', '', $str);
159 $int = (int)($str * 100);
161 $cents = sprintf('.%02d', $cents);
162 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
167 $chars = (string)$int;
170 $index = strlen($chars);
176 $output = ',' . $output;
178 $char = substr($chars, $index, 1);
179 $output = $char . $output;
181 $output = '$' . $output . $cents;
185 function format_dollars($str) {
186 return format_money($str, false);
189 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
190 function format_mdy_to_ymd($str) {
191 require_once('code/wfpl/time.php');
192 return mdy_to_ymd(format_oneline($str));
195 function format_phone($str) {
196 $str = ereg_replace('[^0-9]', '', $str);
197 $str = ereg_replace('^1*', '', $str);
201 if($len < 10 && $len != 7) {
202 #NOT A VALID PHONE NUMBER
207 $output = ' ext: ' . substr($str, 10);
212 $area = substr($str, 0, 3);
213 $str = substr($str, 3);
216 $output = substr($str, 3) . $output;
217 $output = substr($str, 0, 3) . '-' . $output;
220 $output = "($area) " . $output;