3 # Copyright (C) 2005 Jason Woofenden
5 # This file is part of wfpl.
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)
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.
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,
23 # This file contains basic encodings
25 function format_int($str) {
26 return ereg_replace('[^0-9]', '', $str);
29 function format_oneline($str) {
30 $str = str_replace("\r", '', $str);
31 return str_replace("\n", '', $str);
34 function format_unix($str) {
35 return unix_newlines($str);
38 function format_yesno($str) {
46 function format_email($str) {
48 return format_oneline($str);
51 function format_url($str) {
53 return format_oneline($str);
56 function format_money($str, $display_cents = true) {
57 $str = ereg_replace('[^0-9.]', '', $str);
59 $int = (int)($str * 100);
61 $cents = sprintf('.%02d', $cents);
62 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
67 $chars = (string)$int;
70 $index = strlen($chars);
76 $output = ',' . $output;
78 $char = substr($chars, $index, 1);
79 $output = $char . $output;
81 $output = '$' . $output . $cents;
85 function format_dollars($str) {
86 return format_money($str, false);
89 function format_phone($str) {
90 $str = ereg_replace('[^0-9]', '', $str);
91 $str = ereg_replace('^1*', '', $str);
95 if($len < 10 && $len != 7) {
96 #NOT A VALID PHONE NUMBER
101 $output = ' ext: ' . substr($str, 10);
106 $area = substr($str, 0, 3);
107 $str = substr($str, 3);
110 $output = substr($str, 3) . $output;
111 $output = substr($str, 0, 3) . '-' . $output;
114 $output = "($area) " . $output;
121 #function ftest($val) {
122 # printf("$val: '%s'<br />\n", format_phone($val, true));
125 #echo "FORMAT TESTS<br><br>";
133 #ftest("1158221234");
134 #ftest("1558221234");
135 #ftest("12235513234");
136 #ftest("122355123334");
137 #ftest("1585552212334");
138 #ftest("15855522123334");