JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added pulldown support, added leftcheck
[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']), $strict = true)) {
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_zip($str) {
49         return ereg_replace('[^0-9]', '', $str);
50 }
51
52 function format_filename($str) {
53         $str = strtolower($str);
54         $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
55         return ereg_replace('^[.-]', '_', $str);
56 }
57
58 function format_varname($str) {
59         $str = strtolower($str);
60         $str = ereg_replace('[^a-z0-9_]', '_', $str);
61         return ereg_replace('^[0-9]*', '', $str);
62 }
63
64 function format_oneline($str) {
65         $str = str_replace("\r", '', $str);
66         return str_replace("\n", '', $str);
67 }
68
69 function format_unix($str) {
70         return unix_newlines($str);
71 }
72
73 function format_yesno($str) {
74         if($str && $str != 'No') {
75                 return 'Yes';
76         } else {
77                 return 'No';
78         }
79 }
80
81 function format_email($str) {
82         # FIXME
83         return format_oneline($str);
84 }
85
86 function format_url($str) {
87         # FIXME
88         return format_oneline($str);
89 }
90
91 function format_money($str, $display_cents = true) {
92         $str = ereg_replace('[^0-9.]', '', $str);
93         if($display_cents) {
94                 $int = (int)($str * 100);
95                 $cents = $int % 100;
96                 $cents = sprintf('.%02d', $cents);
97                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
98         } else {
99                 $cents = '';
100                 $int = round($str);
101         }
102         $chars = (string)$int;
103         $output = '';
104         $comma = 4;
105         $index = strlen($chars);
106         while($index) {
107                 --$index;
108                 --$comma;
109                 if($comma == 0) {
110                         $comma = 3;
111                         $output = ',' . $output;
112                 }
113                 $char = substr($chars, $index, 1);
114                 $output = $char . $output;
115         }
116         $output = '$' . $output . $cents;
117         return $output;
118 }
119
120 function format_dollars($str) {
121         return format_money($str, false);
122 }
123
124 function format_phone($str) {
125         $str = ereg_replace('[^0-9]', '', $str);
126         $str = ereg_replace('^1*', '', $str);
127         $len = strlen($str);
128         $output = '';
129
130         if($len < 10 && $len != 7) {
131                 #NOT A VALID PHONE NUMBER
132                 return $str;
133         }
134
135         if($len > 10) {
136                 $output = ' ext: ' . substr($str, 10);
137                 $len = 10;
138         }
139
140         if($len == 10) {
141                 $area = substr($str, 0, 3);
142                 $str = substr($str, 3);
143         }
144
145         $output = substr($str, 3) . $output;
146         $output = substr($str, 0, 3) . '-' . $output;
147
148         if($area) {
149                 $output = "($area) " . $output;
150         }
151
152         return $output;
153 }
154
155 ?>