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) {
25 $str = str_replace('_', ' ', $str);
27 return str_replace('Email', 'E-mail', $str);
30 # This function makes sure that $str is in the list of options, and returns "" otherwise
31 function format_options($str, $name) {
32 if(!isset($GLOBALS[$name . '_options'])) {
33 die("Couldn't find options for \"$name\". Be sure to call pulldown().");
36 foreach($GLOBALS[$name . '_options']['options'] as $keyval) {
37 list($key, $value) = $keyval;
46 function format_int($str) {
47 $str = ereg_replace('[^0-9]', '', $str);
48 return ereg_replace('^0*([0-9])', '\1', $str);
51 function format_decimal($str) {
52 $str = ereg_replace('[^0-9.]', '', $str);
53 $pos = strpos($str, '.');
55 $str = str_replace('.', '', $str);
58 } elseif($pos == strlen($str)) {
61 return substr($str, 0, $pos) . '.' . substr($str, $pos);
67 # return 0 of there's no digits
68 function format_int_0($str) {
69 $str = format_int($str);
76 function format_zip($str) {
77 $str = ereg_replace('[^0-9]', '', $str);
78 if(strlen($str) > 5) {
79 return substr($str, 0, 5) . '-' . substr($str, 5);
84 function format_filename($str) {
85 $str = strtolower($str);
86 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
87 return ereg_replace('^[.-]', '_', $str);
90 function format_varname($str) {
91 $str = strtolower($str);
92 $str = ereg_replace('[^a-z0-9_]', '_', $str);
93 return ereg_replace('^[0-9]*', '', $str);
96 function format_oneline($str) {
97 $str = str_replace("\r", '', $str);
98 return str_replace("\n", '', $str);
101 function format_unix($str) {
102 return unix_newlines($str);
105 function format_yesno($str) {
106 if($str && $str != 'No') {
113 function format_email($str) {
115 return format_oneline($str);
118 function format_url($str) {
120 return format_oneline($str);
123 function format_money($str, $display_cents = true) {
124 $str = ereg_replace('[^0-9.]', '', $str);
126 $int = (int)($str * 100);
128 $cents = sprintf('.%02d', $cents);
129 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
134 $chars = (string)$int;
137 $index = strlen($chars);
143 $output = ',' . $output;
145 $char = substr($chars, $index, 1);
146 $output = $char . $output;
148 $output = '$' . $output . $cents;
152 function format_dollars($str) {
153 return format_money($str, false);
156 # date is edited as mm/dd/yyyy but stored as yyyy-mm-dd
157 function format_mdy_to_ymd($str) {
158 return mdy_to_ymd(format_oneline($str));
161 function format_phone($str) {
162 $str = ereg_replace('[^0-9]', '', $str);
163 $str = ereg_replace('^1*', '', $str);
167 if($len < 10 && $len != 7) {
168 #NOT A VALID PHONE NUMBER
173 $output = ' ext: ' . substr($str, 10);
178 $area = substr($str, 0, 3);
179 $str = substr($str, 3);
182 $output = substr($str, 3) . $output;
183 $output = substr($str, 0, 3) . '-' . $output;
186 $output = "($area) " . $output;