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_caption($str) {
26 return str_replace('Email', 'E-mail', $str);
29 function format_int($str) {
30 $str = ereg_replace('[^0-9]', '', $str);
31 return ereg_replace('^0*([1-9])', '\1', $str);
34 function format_zip($str) {
35 return ereg_replace('[^0-9]', '', $str);
38 function format_filename($str) {
39 $str = strtolower($str);
40 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
41 return ereg_replace('^[.-]', '_', $str);
44 function format_varname($str) {
45 $str = strtolower($str);
46 $str = ereg_replace('[^a-z0-9_]', '_', $str);
47 return ereg_replace('^[0-9]*', '', $str);
50 function format_oneline($str) {
51 $str = str_replace("\r", '', $str);
52 return str_replace("\n", '', $str);
55 function format_unix($str) {
56 return unix_newlines($str);
59 function format_yesno($str) {
60 if($str && $str != 'No') {
67 function format_email($str) {
69 return format_oneline($str);
72 function format_url($str) {
74 return format_oneline($str);
77 function format_money($str, $display_cents = true) {
78 $str = ereg_replace('[^0-9.]', '', $str);
80 $int = (int)($str * 100);
82 $cents = sprintf('.%02d', $cents);
83 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
88 $chars = (string)$int;
91 $index = strlen($chars);
97 $output = ',' . $output;
99 $char = substr($chars, $index, 1);
100 $output = $char . $output;
102 $output = '$' . $output . $cents;
106 function format_dollars($str) {
107 return format_money($str, false);
110 function format_phone($str) {
111 $str = ereg_replace('[^0-9]', '', $str);
112 $str = ereg_replace('^1*', '', $str);
116 if($len < 10 && $len != 7) {
117 #NOT A VALID PHONE NUMBER
122 $output = ' ext: ' . substr($str, 10);
127 $area = substr($str, 0, 3);
128 $str = substr($str, 3);
131 $output = substr($str, 3) . $output;
132 $output = substr($str, 0, 3) . '-' . $output;
135 $output = "($area) " . $output;