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 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)
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
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
22 # This file contains basic encodings
24 function format_int($str) {
25 $str = ereg_replace('[^0-9]', '', $str);
26 return ereg_replace('^0*([1-9])', '\1', $str);
29 function format_filename($str) {
30 $str = strtolower($str);
31 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
32 return ereg_replace('^[0-9.-]*', '', $str);
35 function format_varname($str) {
36 $str = strtolower($str);
37 $str = ereg_replace('[^a-z0-9_]', '_', $str);
38 return ereg_replace('^[0-9]*', '', $str);
41 function format_oneline($str) {
42 $str = str_replace("\r", '', $str);
43 return str_replace("\n", '', $str);
46 function format_unix($str) {
47 return unix_newlines($str);
50 function format_yesno($str) {
58 function format_email($str) {
60 return format_oneline($str);
63 function format_url($str) {
65 return format_oneline($str);
68 function format_money($str, $display_cents = true) {
69 $str = ereg_replace('[^0-9.]', '', $str);
71 $int = (int)($str * 100);
73 $cents = sprintf('.%02d', $cents);
74 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
79 $chars = (string)$int;
82 $index = strlen($chars);
88 $output = ',' . $output;
90 $char = substr($chars, $index, 1);
91 $output = $char . $output;
93 $output = '$' . $output . $cents;
97 function format_dollars($str) {
98 return format_money($str, false);
101 function format_phone($str) {
102 $str = ereg_replace('[^0-9]', '', $str);
103 $str = ereg_replace('^1*', '', $str);
107 if($len < 10 && $len != 7) {
108 #NOT A VALID PHONE NUMBER
113 $output = ' ext: ' . substr($str, 10);
118 $area = substr($str, 0, 3);
119 $str = substr($str, 3);
122 $output = substr($str, 3) . $output;
123 $output = substr($str, 0, 3) . '-' . $output;
126 $output = "($area) " . $output;