JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed pulldowns, metaform suppors zip and decimal, run.php supports straight html...
[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         if(!in_array($str, array_keys($GLOBALS[$name . '_options']['options']))) {
37                 return '';
38         }
39
40         return $str;
41 }
42
43 function format_int($str) {
44         $str = ereg_replace('[^0-9]', '', $str);
45         return ereg_replace('^0*([1-9])', '\1', $str);
46 }
47
48 function format_decimal($str) {
49         $str = ereg_replace('[^0-9.]', '', $str);
50         $pos = strpos($str, '.');
51         if($pos !== false) {
52                 $str = str_replace('.', '', $str);
53                 if($pos == 0) {
54                         return '0.' . $str;
55                 } elseif($pos == strlen($str)) {
56                         return $str;
57                 } else {
58                         return substr($str, 0, $pos) . '.' . substr($str, $pos);
59                 }
60         }
61         return $str;
62 }
63
64 # return 0 of there's no digits
65 function format_int_0($str) {
66         $str = format_int($str);
67         if($str == '') {
68                 return '0';
69         }
70         return $str;
71 }
72
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);
77         }
78         return $str;
79 }
80
81 function format_filename($str) {
82         $str = strtolower($str);
83         $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
84         return ereg_replace('^[.-]', '_', $str);
85 }
86
87 function format_varname($str) {
88         $str = strtolower($str);
89         $str = ereg_replace('[^a-z0-9_]', '_', $str);
90         return ereg_replace('^[0-9]*', '', $str);
91 }
92
93 function format_oneline($str) {
94         $str = str_replace("\r", '', $str);
95         return str_replace("\n", '', $str);
96 }
97
98 function format_unix($str) {
99         return unix_newlines($str);
100 }
101
102 function format_yesno($str) {
103         if($str && $str != 'No') {
104                 return 'Yes';
105         } else {
106                 return 'No';
107         }
108 }
109
110 function format_email($str) {
111         # FIXME
112         return format_oneline($str);
113 }
114
115 function format_url($str) {
116         # FIXME
117         return format_oneline($str);
118 }
119
120 function format_money($str, $display_cents = true) {
121         $str = ereg_replace('[^0-9.]', '', $str);
122         if($display_cents) {
123                 $int = (int)($str * 100);
124                 $cents = $int % 100;
125                 $cents = sprintf('.%02d', $cents);
126                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
127         } else {
128                 $cents = '';
129                 $int = round($str);
130         }
131         $chars = (string)$int;
132         $output = '';
133         $comma = 4;
134         $index = strlen($chars);
135         while($index) {
136                 --$index;
137                 --$comma;
138                 if($comma == 0) {
139                         $comma = 3;
140                         $output = ',' . $output;
141                 }
142                 $char = substr($chars, $index, 1);
143                 $output = $char . $output;
144         }
145         $output = '$' . $output . $cents;
146         return $output;
147 }
148
149 function format_dollars($str) {
150         return format_money($str, false);
151 }
152
153 function format_phone($str) {
154         $str = ereg_replace('[^0-9]', '', $str);
155         $str = ereg_replace('^1*', '', $str);
156         $len = strlen($str);
157         $output = '';
158
159         if($len < 10 && $len != 7) {
160                 #NOT A VALID PHONE NUMBER
161                 return $str;
162         }
163
164         if($len > 10) {
165                 $output = ' ext: ' . substr($str, 10);
166                 $len = 10;
167         }
168
169         if($len == 10) {
170                 $area = substr($str, 0, 3);
171                 $str = substr($str, 3);
172         }
173
174         $output = substr($str, 3) . $output;
175         $output = substr($str, 0, 3) . '-' . $output;
176
177         if($area) {
178                 $output = "($area) " . $output;
179         }
180
181         return $output;
182 }
183
184 ?>