JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
initial import (from revision 1199 + email.php)
[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_money($str, $display_cents = true) {
26         $str = ereg_replace('[^0-9.]', '', $str);
27         if($display_cents) {
28                 $int = (int)($str * 100);
29                 $cents = $int % 100;
30                 $cents = sprintf('.%02d', $cents);
31                 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
32         } else {
33                 $cents = '';
34                 $int = round($str);
35         }
36         $chars = (string)$int;
37         $output = '';
38         $comma = 4;
39         $index = strlen($chars);
40         while($index) {
41                 --$index;
42                 --$comma;
43                 if($comma == 0) {
44                         $comma = 3;
45                         $output = ',' . $output;
46                 }
47                 $char = substr($chars, $index, 1);
48                 $output = $char . $output;
49         }
50         $output = '$' . $output . $cents;
51         return $output;
52 }
53
54 function format_dollars($str) {
55         return format_money($str, false);
56 }
57
58 function format_phone($str) {
59         $str = ereg_replace('[^0-9]', '', $str);
60         $str = ereg_replace('^1*', '', $str);
61         $len = strlen($str);
62         $output = '';
63
64         if($len < 10 && $len != 7) {
65                 #NOT A VALID PHONE NUMBER
66                 return $str;
67         }
68
69         if($len > 10) {
70                 $output = ' ext: ' . substr($str, 10);
71                 $len = 10;
72         }
73
74         if($len == 10) {
75                 $area = substr($str, 0, 3);
76                 $str = substr($str, 3);
77         }
78
79         $output = substr($str, 3) . $output;
80         $output = substr($str, 0, 3) . '-' . $output;
81
82         if($area) {
83                 $output = "($area) " . $output;
84         }
85
86         return $output;
87 }
88
89
90 #function ftest($val) {
91 #       printf("$val: '%s'<br />\n", format_phone($val, true));
92 #}
93 #
94 #echo "FORMAT TESTS<br><br>";
95 #ftest("$3");
96 #ftest("3.99");
97 #ftest("3.5");
98 #ftest("891234");
99 #ftest("8221234");
100 #ftest("82212334");
101 #ftest("122313234");
102 #ftest("1158221234");
103 #ftest("1558221234");
104 #ftest("12235513234");
105 #ftest("122355123334");
106 #ftest("1585552212334");
107 #ftest("15855522123334");
108
109 ?>