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']))) {
43 function format_int($str) {
44 $str = ereg_replace('[^0-9]', '', $str);
45 return ereg_replace('^0*([1-9])', '\1', $str);
48 function format_decimal($str) {
49 $str = ereg_replace('[^0-9.]', '', $str);
50 $pos = strpos($str, '.');
52 $str = str_replace('.', '', $str);
55 } elseif($pos == strlen($str)) {
58 return substr($str, 0, $pos) . '.' . substr($str, $pos);
64 # return 0 of there's no digits
65 function format_int_0($str) {
66 $str = format_int($str);
73 function format_zip($str) {
74 $str = ereg_replace('[^0-9]', '', $str);
75 if(strlen($str) > 5) {
76 return substr($str, 0, 5) . '-' . substr($str, 5);
81 function format_filename($str) {
82 $str = strtolower($str);
83 $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
84 return ereg_replace('^[.-]', '_', $str);
87 function format_varname($str) {
88 $str = strtolower($str);
89 $str = ereg_replace('[^a-z0-9_]', '_', $str);
90 return ereg_replace('^[0-9]*', '', $str);
93 function format_oneline($str) {
94 $str = str_replace("\r", '', $str);
95 return str_replace("\n", '', $str);
98 function format_unix($str) {
99 return unix_newlines($str);
102 function format_yesno($str) {
103 if($str && $str != 'No') {
110 function format_email($str) {
112 return format_oneline($str);
115 function format_url($str) {
117 return format_oneline($str);
120 function format_money($str, $display_cents = true) {
121 $str = ereg_replace('[^0-9.]', '', $str);
123 $int = (int)($str * 100);
125 $cents = sprintf('.%02d', $cents);
126 $int = (int)($str); # go from the source again, so we can print numbers above 2M without cents.
131 $chars = (string)$int;
134 $index = strlen($chars);
140 $output = ',' . $output;
142 $char = substr($chars, $index, 1);
143 $output = $char . $output;
145 $output = '$' . $output . $cents;
149 function format_dollars($str) {
150 return format_money($str, false);
153 function format_phone($str) {
154 $str = ereg_replace('[^0-9]', '', $str);
155 $str = ereg_replace('^1*', '', $str);
159 if($len < 10 && $len != 7) {
160 #NOT A VALID PHONE NUMBER
165 $output = ' ext: ' . substr($str, 10);
170 $area = substr($str, 0, 3);
171 $str = substr($str, 3);
174 $output = substr($str, 3) . $output;
175 $output = substr($str, 0, 3) . '-' . $output;
178 $output = "($area) " . $output;