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) {
147 return format_oneline($str);
150 function format_money($str, $display_cents = true) {
151 $str = ereg_replace('[^0-9.]', '', $str);
153 $int = (int)($str * 100);
155 $cents = sprintf('.%02d', $cents);
156 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
161 $chars = (string)$int;
164 $index = strlen($chars);
170 $output = ',' . $output;
172 $char = substr($chars, $index, 1);
173 $output = $char . $output;
175 $output = '$' . $output . $cents;
179 function format_dollars($str) {
180 return format_money($str, false);
183 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
184 function format_mdy_to_ymd($str) {
185 require_once('code/wfpl/time.php');
186 return mdy_to_ymd(format_oneline($str));
189 function format_phone($str) {
190 $str = ereg_replace('[^0-9]', '', $str);
191 $str = ereg_replace('^1*', '', $str);
195 if($len < 10 && $len != 7) {
196 #NOT A VALID PHONE NUMBER
201 $output = ' ext: ' . substr($str, 10);
206 $area = substr($str, 0, 3);
207 $str = substr($str, 3);
210 $output = substr($str, 3) . $output;
211 $output = substr($str, 0, 3) . '-' . $output;
214 $output = "($area) " . $output;