JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added missing format_url needed by metaform
[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
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file contains basic encodings
24
25 function format_oneline($str) {
26         $str = str_replace("\r", '', $str);
27         return str_replace("\n", '', $str);
28 }
29
30 function format_unix($str) {
31         return unix_newlines($str);
32 }
33
34 function format_yesno($str) {
35         if($str) {
36                 return "Yes";
37         } else {
38                 return "No";
39         }
40 }
41
42 function format_email($str) {
43         # FIXME
44         return format_oneline($str);
45 }
46
47 function format_url($str) {
48         # FIXME
49         return format_oneline($str);
50 }
51
52 function format_money($str, $display_cents = true) {
53         $str = ereg_replace('[^0-9.]', '', $str);
54         if($display_cents) {
55                 $int = (int)($str * 100);
56                 $cents = $int % 100;
57                 $cents = sprintf('.%02d', $cents);
58                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
59         } else {
60                 $cents = '';
61                 $int = round($str);
62         }
63         $chars = (string)$int;
64         $output = '';
65         $comma = 4;
66         $index = strlen($chars);
67         while($index) {
68                 --$index;
69                 --$comma;
70                 if($comma == 0) {
71                         $comma = 3;
72                         $output = ',' . $output;
73                 }
74                 $char = substr($chars, $index, 1);
75                 $output = $char . $output;
76         }
77         $output = '$' . $output . $cents;
78         return $output;
79 }
80
81 function format_dollars($str) {
82         return format_money($str, false);
83 }
84
85 function format_phone($str) {
86         $str = ereg_replace('[^0-9]', '', $str);
87         $str = ereg_replace('^1*', '', $str);
88         $len = strlen($str);
89         $output = '';
90
91         if($len < 10 && $len != 7) {
92                 #NOT A VALID PHONE NUMBER
93                 return $str;
94         }
95
96         if($len > 10) {
97                 $output = ' ext: ' . substr($str, 10);
98                 $len = 10;
99         }
100
101         if($len == 10) {
102                 $area = substr($str, 0, 3);
103                 $str = substr($str, 3);
104         }
105
106         $output = substr($str, 3) . $output;
107         $output = substr($str, 0, 3) . '-' . $output;
108
109         if($area) {
110                 $output = "($area) " . $output;
111         }
112
113         return $output;
114 }
115
116
117 #function ftest($val) {
118 #       printf("$val: '%s'<br />\n", format_phone($val, true));
119 #}
120 #
121 #echo "FORMAT TESTS<br><br>";
122 #ftest("$3");
123 #ftest("3.99");
124 #ftest("3.5");
125 #ftest("891234");
126 #ftest("8221234");
127 #ftest("82212334");
128 #ftest("122313234");
129 #ftest("1158221234");
130 #ftest("1558221234");
131 #ftest("12235513234");
132 #ftest("122355123334");
133 #ftest("1585552212334");
134 #ftest("15855522123334");
135
136 ?>