JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added: format_h_w_image()
[wfpl.git] / format.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file contains basic encodings
23
24 function format_caption($str) {
25         $str = str_replace('_', ' ', $str);
26         $str = ucwords($str);
27         return str_replace('Email', 'E-mail', $str);
28 }
29
30 # This function makes sure that $str is in the list of options, and returns "" otherwise
31 function format_options($str, $name) {
32         if(!isset($GLOBALS[$name . '_options'])) {
33                 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
34         }
35
36         foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
37                 list($key, $value) = $keyval;
38                 if($str == $key) {
39                         return $str;
40                 }
41         }
42
43         return $str;
44 }
45
46 function format_int($str) {
47         $str = ereg_replace('[^0-9]', '', $str);
48         return ereg_replace('^0*([0-9])', '\1', $str);
49 }
50
51 function format_decimal($str) {
52         $str = ereg_replace('[^0-9.]', '', $str);
53         $pos = strpos($str, '.');
54         if($pos !== false) {
55                 $str = str_replace('.', '', $str);
56                 if($pos == 0) {
57                         return '0.' . $str;
58                 } elseif($pos == strlen($str)) {
59                         return $str;
60                 } else {
61                         return substr($str, 0, $pos) . '.' . substr($str, $pos);
62                 }
63         }
64         return $str;
65 }
66
67 # return 0 of there's no digits
68 function format_int_0($str) {
69         $str = format_int($str);
70         if($str == '') {
71                 return '0';
72         }
73         return $str;
74 }
75
76 function format_zip($str) {
77         $str = ereg_replace('[^0-9]', '', $str);
78         if(strlen($str) > 5) {
79                 return substr($str, 0, 5) . '-' . substr($str, 5);
80         }
81         return $str;
82 }
83
84 function format_filename($str) {
85         $str = strtolower($str);
86         $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
87         return ereg_replace('^[.-]', '_', $str);
88 }
89
90 function format_h_w_image($str) {
91         $fields = explode(' ', $str);
92         if(count($fields) != 3) {
93                 return '';
94         }
95
96         list($width, $height, $filename) = $fields;
97         $width = format_int_0($width);
98         $height = format_int_0($height);
99         $filename = format_filename($filename);
100
101         return "$width $height $filename";
102 }
103
104 function format_varname($str) {
105         $str = strtolower($str);
106         $str = ereg_replace('[^a-z0-9_]', '_', $str);
107         return ereg_replace('^[0-9]*', '', $str);
108 }
109
110 function format_oneline($str) {
111         $str = str_replace("\r", '', $str);
112         return str_replace("\n", '', $str);
113 }
114
115 function format_unix($str) {
116         return unix_newlines($str);
117 }
118
119 function format_yesno($str) {
120         if($str && $str != 'No') {
121                 return 'Yes';
122         } else {
123                 return 'No';
124         }
125 }
126
127 function format_email($str) {
128         # FIXME
129         return trim(format_oneline($str));
130 }
131
132 function format_url($str) {
133         # FIXME
134         return format_oneline($str);
135 }
136
137 function format_money($str, $display_cents = true) {
138         $str = ereg_replace('[^0-9.]', '', $str);
139         if($display_cents) {
140                 $int = (int)($str * 100);
141                 $cents = $int % 100;
142                 $cents = sprintf('.%02d', $cents);
143                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
144         } else {
145                 $cents = '';
146                 $int = round($str);
147         }
148         $chars = (string)$int;
149         $output = '';
150         $comma = 4;
151         $index = strlen($chars);
152         while($index) {
153                 --$index;
154                 --$comma;
155                 if($comma == 0) {
156                         $comma = 3;
157                         $output = ',' . $output;
158                 }
159                 $char = substr($chars, $index, 1);
160                 $output = $char . $output;
161         }
162         $output = '$' . $output . $cents;
163         return $output;
164 }
165
166 function format_dollars($str) {
167         return format_money($str, false);
168 }
169
170 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
171 function format_mdy_to_ymd($str) {
172         return mdy_to_ymd(format_oneline($str));
173 }
174
175 function format_phone($str) {
176         $str = ereg_replace('[^0-9]', '', $str);
177         $str = ereg_replace('^1*', '', $str);
178         $len = strlen($str);
179         $output = '';
180
181         if($len < 10 && $len != 7) {
182                 #NOT A VALID PHONE NUMBER
183                 return $str;
184         }
185
186         if($len > 10) {
187                 $output = ' ext: ' . substr($str, 10);
188                 $len = 10;
189         }
190
191         if($len == 10) {
192                 $area = substr($str, 0, 3);
193                 $str = substr($str, 3);
194         }
195
196         $output = substr($str, 3) . $output;
197         $output = substr($str, 0, 3) . '-' . $output;
198
199         if($area) {
200                 $output = "($area) " . $output;
201         }
202
203         return $output;
204 }
205
206 ?>