JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added format_varname() and format_filename()
[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_int($str) {
25         $str = ereg_replace('[^0-9]', '', $str);
26         return ereg_replace('^0*([1-9])', '\1', $str);
27 }
28
29 function format_filename($str) {
30         $str = strtolower($str);
31         $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
32         return ereg_replace('^[0-9.-]*', '', $str);
33 }
34
35 function format_varname($str) {
36         $str = strtolower($str);
37         $str = ereg_replace('[^a-z0-9_]', '_', $str);
38         return ereg_replace('^[0-9]*', '', $str);
39 }
40
41 function format_oneline($str) {
42         $str = str_replace("\r", '', $str);
43         return str_replace("\n", '', $str);
44 }
45
46 function format_unix($str) {
47         return unix_newlines($str);
48 }
49
50 function format_yesno($str) {
51         if($str) {
52                 return "Yes";
53         } else {
54                 return "No";
55         }
56 }
57
58 function format_email($str) {
59         # FIXME
60         return format_oneline($str);
61 }
62
63 function format_url($str) {
64         # FIXME
65         return format_oneline($str);
66 }
67
68 function format_money($str, $display_cents = true) {
69         $str = ereg_replace('[^0-9.]', '', $str);
70         if($display_cents) {
71                 $int = (int)($str * 100);
72                 $cents = $int % 100;
73                 $cents = sprintf('.%02d', $cents);
74                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
75         } else {
76                 $cents = '';
77                 $int = round($str);
78         }
79         $chars = (string)$int;
80         $output = '';
81         $comma = 4;
82         $index = strlen($chars);
83         while($index) {
84                 --$index;
85                 --$comma;
86                 if($comma == 0) {
87                         $comma = 3;
88                         $output = ',' . $output;
89                 }
90                 $char = substr($chars, $index, 1);
91                 $output = $char . $output;
92         }
93         $output = '$' . $output . $cents;
94         return $output;
95 }
96
97 function format_dollars($str) {
98         return format_money($str, false);
99 }
100
101 function format_phone($str) {
102         $str = ereg_replace('[^0-9]', '', $str);
103         $str = ereg_replace('^1*', '', $str);
104         $len = strlen($str);
105         $output = '';
106
107         if($len < 10 && $len != 7) {
108                 #NOT A VALID PHONE NUMBER
109                 return $str;
110         }
111
112         if($len > 10) {
113                 $output = ' ext: ' . substr($str, 10);
114                 $len = 10;
115         }
116
117         if($len == 10) {
118                 $area = substr($str, 0, 3);
119                 $str = substr($str, 3);
120         }
121
122         $output = substr($str, 3) . $output;
123         $output = substr($str, 0, 3) . '-' . $output;
124
125         if($area) {
126                 $output = "($area) " . $output;
127         }
128
129         return $output;
130 }
131
132 ?>