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_zip($str) {
30 return ereg_replace('[^0-9]', '', $str);
33 function format_filename($str) {
34 $str = strtolower($str);
35 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
36 return ereg_replace('^[0-9.-]*', '', $str);
39 function format_varname($str) {
40 $str = strtolower($str);
41 $str = ereg_replace('[^a-z0-9_]', '_', $str);
42 return ereg_replace('^[0-9]*', '', $str);
45 function format_oneline($str) {
46 $str = str_replace("\r", '', $str);
47 return str_replace("\n", '', $str);
50 function format_unix($str) {
51 return unix_newlines($str);
54 function format_yesno($str) {
62 function format_email($str) {
64 return format_oneline($str);
67 function format_url($str) {
69 return format_oneline($str);
72 function format_money($str, $display_cents = true) {
73 $str = ereg_replace('[^0-9.]', '', $str);
75 $int = (int)($str * 100);
77 $cents = sprintf('.%02d', $cents);
78 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
83 $chars = (string)$int;
86 $index = strlen($chars);
92 $output = ',' . $output;
94 $char = substr($chars, $index, 1);
95 $output = $char . $output;
97 $output = '$' . $output . $cents;
101 function format_dollars($str) {
102 return format_money($str, false);
105 function format_phone($str) {
106 $str = ereg_replace('[^0-9]', '', $str);
107 $str = ereg_replace('^1*', '', $str);
111 if($len < 10 && $len != 7) {
112 #NOT A VALID PHONE NUMBER
117 $output = ' ext: ' . substr($str, 10);
122 $area = substr($str, 0, 3);
123 $str = substr($str, 3);
126 $output = substr($str, 3) . $output;
127 $output = substr($str, 0, 3) . '-' . $output;
130 $output = "($area) " . $output;