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 if(!in_array($str, array_keys($GLOBALS[$name . '_options']['options']), $strict = true)) {
43 function format_int($str) {
44 $str = ereg_replace('[^0-9]', '', $str);
45 return ereg_replace('^0*([1-9])', '\1', $str);
48 function format_zip($str) {
49 return ereg_replace('[^0-9]', '', $str);
52 function format_filename($str) {
53 $str = strtolower($str);
54 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
55 return ereg_replace('^[.-]', '_', $str);
58 function format_varname($str) {
59 $str = strtolower($str);
60 $str = ereg_replace('[^a-z0-9_]', '_', $str);
61 return ereg_replace('^[0-9]*', '', $str);
64 function format_oneline($str) {
65 $str = str_replace("\r", '', $str);
66 return str_replace("\n", '', $str);
69 function format_unix($str) {
70 return unix_newlines($str);
73 function format_yesno($str) {
74 if($str && $str != 'No') {
81 function format_email($str) {
83 return format_oneline($str);
86 function format_url($str) {
88 return format_oneline($str);
91 function format_money($str, $display_cents = true) {
92 $str = ereg_replace('[^0-9.]', '', $str);
94 $int = (int)($str * 100);
96 $cents = sprintf('.%02d', $cents);
97 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
102 $chars = (string)$int;
105 $index = strlen($chars);
111 $output = ',' . $output;
113 $char = substr($chars, $index, 1);
114 $output = $char . $output;
116 $output = '$' . $output . $cents;
120 function format_dollars($str) {
121 return format_money($str, false);
124 function format_phone($str) {
125 $str = ereg_replace('[^0-9]', '', $str);
126 $str = ereg_replace('^1*', '', $str);
130 if($len < 10 && $len != 7) {
131 #NOT A VALID PHONE NUMBER
136 $output = ' ext: ' . substr($str, 10);
141 $area = substr($str, 0, 3);
142 $str = substr($str, 3);
145 $output = substr($str, 3) . $output;
146 $output = substr($str, 0, 3) . '-' . $output;
149 $output = "($area) " . $output;