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