1 |
dashley |
35 |
<?php
|
2 |
|
|
//********************************************************************************
|
3 |
|
|
//Copyright (C) 2007 David T. Ashley
|
4 |
|
|
//********************************************************************************
|
5 |
|
|
//This program or source file is free software; you can redistribute it and/or
|
6 |
|
|
//modify it under the terms of the GNU Lesser General Public License as published
|
7 |
|
|
//by the Free Software Foundation; either version 2 of the License, or (at your
|
8 |
|
|
//option) any later version.
|
9 |
|
|
//
|
10 |
|
|
//This program or source file is distributed in the hope that it will
|
11 |
|
|
//be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
//GNU General Public License for more details.
|
14 |
|
|
//
|
15 |
|
|
//You may have received a copy of the GNU Lesser General Public License
|
16 |
|
|
//along with this program; if not, write to the Free Software
|
17 |
|
|
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
|
|
//********************************************************************************
|
19 |
|
|
//Utility functions that operate on strings.
|
20 |
|
|
//--------------------------------------------------------------------------------
|
21 |
|
|
//Pads a string on the left by zero until it reaches the
|
22 |
|
|
//target length. If the string is longer than the target length,
|
23 |
|
|
//it is truncated on the left.
|
24 |
|
|
//
|
25 |
|
|
//Unit tested 20060408.
|
26 |
|
|
//
|
27 |
|
|
function STRFUNC_pad_left_zero($str_in, $target_len)
|
28 |
|
|
{
|
29 |
|
|
if ($target_len >= 0) //Ignore invalid targets
|
30 |
|
|
{
|
31 |
|
|
$len = strlen($str_in);
|
32 |
|
|
|
33 |
|
|
if ($target_len == 0)
|
34 |
|
|
{
|
35 |
|
|
$str_in = "";
|
36 |
|
|
}
|
37 |
|
|
else if ($len < $target_len)
|
38 |
|
|
{
|
39 |
|
|
//The input string is too short. Pad it on the left with zeros.
|
40 |
|
|
$to_add = $target_len - $len;
|
41 |
|
|
for ($i = 0; $i < $to_add; $i++)
|
42 |
|
|
{
|
43 |
|
|
$str_in = "0". $str_in;
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
else if ($len > $target_len)
|
47 |
|
|
{
|
48 |
|
|
//The input string is too long. Remove the extra characters.
|
49 |
|
|
$to_remove = $len - $target_len;
|
50 |
|
|
|
51 |
|
|
$str_in = substr($str_in, $to_remove);
|
52 |
|
|
}
|
53 |
|
|
//In the final omitted else case, there is length equality, and hence nothing to do.
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
return($str_in);
|
57 |
|
|
}
|
58 |
|
|
//
|
59 |
|
|
//
|
60 |
|
|
//--------------------------------------------------------------------------------
|
61 |
|
|
//Returns 1 if the string is purely digits '0'-'9', or 0 otherwise.
|
62 |
|
|
//
|
63 |
|
|
//Unit tested 20060408.
|
64 |
|
|
//
|
65 |
|
|
function STRFUNC_is_pure_digits($arg)
|
66 |
|
|
{
|
67 |
|
|
//Must be a string.
|
68 |
|
|
if (! is_string($arg))
|
69 |
|
|
return(0);
|
70 |
|
|
|
71 |
|
|
$len = strlen($arg);
|
72 |
|
|
|
73 |
|
|
for ($i=0; $i<$len; $i++)
|
74 |
|
|
{
|
75 |
|
|
$c = SubStr($arg, $i, 1);
|
76 |
|
|
if (strpos("0123456789", $c) === FALSE)
|
77 |
|
|
return(0);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
return(1);
|
81 |
|
|
}
|
82 |
|
|
//
|
83 |
|
|
//
|
84 |
|
|
//--------------------------------------------------------------------------------
|
85 |
|
|
//Returns 1 if each character in the subset is present in the set, i.e.
|
86 |
|
|
//if it is a proper or improper subset, or 0 otherwise.
|
87 |
|
|
//
|
88 |
|
|
//Unit tested 20060408.
|
89 |
|
|
//
|
90 |
|
|
function STRFUNC_is_char_subset($conjectured_subset, $reference_set)
|
91 |
|
|
{
|
92 |
|
|
$conjsubsetlen = strlen($conjectured_subset);
|
93 |
|
|
$refsetlen = strlen($reference_set);
|
94 |
|
|
|
95 |
|
|
for ($i=0; $i<$conjsubsetlen; $i++)
|
96 |
|
|
{
|
97 |
|
|
$c = SubStr($conjectured_subset, $i, 1);
|
98 |
|
|
|
99 |
|
|
if (strpos($reference_set, $c) === FALSE)
|
100 |
|
|
return(0);
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
//If we've made it this far, no character has failed to pan out.
|
104 |
|
|
return(1);
|
105 |
|
|
}
|
106 |
|
|
//--------------------------------------------------------------------------------
|
107 |
|
|
//Removes all characters from the input that are not in the set of
|
108 |
|
|
//allowed characters.
|
109 |
|
|
//
|
110 |
|
|
function STRFUNC_force_into_subset($input, $subset)
|
111 |
|
|
{
|
112 |
|
|
$inputlen = strlen($input);
|
113 |
|
|
$subsetlen = strlen($subset);
|
114 |
|
|
|
115 |
|
|
$rv = "";
|
116 |
|
|
for ($i=0; $i<$inputlen; $i++)
|
117 |
|
|
{
|
118 |
|
|
$c = SubStr($input, $i, 1);
|
119 |
|
|
|
120 |
|
|
if (strpos($subset, $c) === FALSE)
|
121 |
|
|
{
|
122 |
|
|
//Character is not in the set. Do not add it to
|
123 |
|
|
//the result.
|
124 |
|
|
}
|
125 |
|
|
else
|
126 |
|
|
{
|
127 |
|
|
//Character is in the set. Add it.
|
128 |
|
|
$rv .= $c;
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
return($rv);
|
133 |
|
|
}
|
134 |
|
|
//
|
135 |
|
|
//--------------------------------------------------------------------------------
|
136 |
|
|
//Forces the variable to be a string, removes all characters from the input that
|
137 |
|
|
//are not in the set of allowed characters, then truncates the string if it is
|
138 |
|
|
//too long.
|
139 |
|
|
//
|
140 |
|
|
function STRFUNC_force_stringtype_subset_truncate($input, $subset, $maxlen)
|
141 |
|
|
{
|
142 |
|
|
//Force the type. Only numerics and strings can reliably be
|
143 |
|
|
//strings.
|
144 |
|
|
//
|
145 |
|
|
if (is_string($input))
|
146 |
|
|
{
|
147 |
|
|
//It is already a string. Do nothing.
|
148 |
|
|
}
|
149 |
|
|
else if (is_numeric($input))
|
150 |
|
|
{
|
151 |
|
|
//A number can be reliably made to a string.
|
152 |
|
|
$input = (string) $input;
|
153 |
|
|
}
|
154 |
|
|
else
|
155 |
|
|
{
|
156 |
|
|
//We don't know what it is. Make the empty string out of it.
|
157 |
|
|
$input = (string) "";
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
//Force it into the allowed character set.
|
161 |
|
|
$input = STRFUNC_force_into_subset($input, $subset);
|
162 |
|
|
|
163 |
|
|
//Take care of the length.
|
164 |
|
|
if (strlen($input) > $maxlen)
|
165 |
|
|
$input = SubStr($input, 0, $maxlen);
|
166 |
|
|
|
167 |
|
|
//echo " / " . $input . " / ";
|
168 |
|
|
|
169 |
|
|
return($input);
|
170 |
|
|
}
|
171 |
|
|
//
|
172 |
|
|
//--------------------------------------------------------------------------------
|
173 |
|
|
//For the passed string, will return a string of " "'s that approximately
|
174 |
|
|
//equals the length when displayed in a table.
|
175 |
|
|
//
|
176 |
|
|
function STRFUNC_nbsp_padding($arg)
|
177 |
|
|
{
|
178 |
|
|
$n = (int)(1.95 * strlen($arg));
|
179 |
|
|
|
180 |
|
|
$rv = "";
|
181 |
|
|
|
182 |
|
|
for ($i = 0; $i < $n; $i++)
|
183 |
|
|
{
|
184 |
|
|
$rv .= " ";
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
return($rv);
|
188 |
|
|
}
|
189 |
|
|
//
|
190 |
|
|
//--------------------------------------------------------------------------------
|
191 |
|
|
//Escapes a JavaScript internal string that is delimited by single quotes.
|
192 |
|
|
//This allows the use of contractions and other constructs within the
|
193 |
|
|
//string.
|
194 |
|
|
//
|
195 |
|
|
//The string is not truncated in any way (it should be known in advance
|
196 |
|
|
//that the string is of a suitable length).
|
197 |
|
|
//
|
198 |
|
|
//This function is used to preprocess strings (usually from a database) that
|
199 |
|
|
//will occur embedded in JavaScript within single quotes.
|
200 |
|
|
//
|
201 |
|
|
function STRFUNC_js_escape_sglquote_notrunc($arg)
|
202 |
|
|
{
|
203 |
|
|
//Be sure no funky stuff in string.
|
204 |
|
|
$temp = STRFUNC_force_into_subset
|
205 |
|
|
(
|
206 |
|
|
$arg,
|
207 |
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ;.,()<>{}:'\"-*+/0123456789"
|
208 |
|
|
);
|
209 |
|
|
//
|
210 |
|
|
//For each single quote in string, replace it with \'. For each double-quote,
|
211 |
|
|
//since I haven't figured out how to properly escape it, it is turned into an
|
212 |
|
|
//escaped single quote.
|
213 |
|
|
$rv = "";
|
214 |
|
|
$count = strlen($temp);
|
215 |
|
|
for ($i=0; $i<$count; $i++)
|
216 |
|
|
{
|
217 |
|
|
$c = SubStr($temp, $i, 1);
|
218 |
|
|
if ($c == "'")
|
219 |
|
|
$rv .= "\\'";
|
220 |
|
|
else if ($c == "\"")
|
221 |
|
|
$rv .= "\\'";
|
222 |
|
|
else
|
223 |
|
|
$rv .= $c;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
return($rv);
|
227 |
|
|
}
|
228 |
|
|
//
|
229 |
|
|
//--------------------------------------------------------------------------------
|
230 |
|
|
//Escapes an HTML "title" string that is delimited by double quotes.
|
231 |
|
|
//This allows the use of quoted text and other constructs within the
|
232 |
|
|
//string.
|
233 |
|
|
//
|
234 |
|
|
//The string is not truncated in any way (it should be known in advance
|
235 |
|
|
//that the string is of a suitable length).
|
236 |
|
|
//
|
237 |
|
|
function STRFUNC_html_title_escape_dblquote_notrunc($arg)
|
238 |
|
|
{
|
239 |
|
|
//Be sure no funky stuff in string.
|
240 |
|
|
$temp = STRFUNC_force_into_subset
|
241 |
|
|
(
|
242 |
|
|
$arg,
|
243 |
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ;.,()<>{}:'\"-*+/0123456789"
|
244 |
|
|
);
|
245 |
|
|
//
|
246 |
|
|
//For each double quote in string, replace it with \".
|
247 |
|
|
$rv = "";
|
248 |
|
|
$count = strlen($temp);
|
249 |
|
|
for ($i=0; $i<$count; $i++)
|
250 |
|
|
{
|
251 |
|
|
$c = SubStr($temp, $i, 1);
|
252 |
|
|
if ($c == "\"")
|
253 |
|
|
$rv .= """;
|
254 |
|
|
else
|
255 |
|
|
$rv .= $c;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
return($rv);
|
259 |
|
|
}
|
260 |
|
|
//
|
261 |
|
|
//--------------------------------------------------------------------------------
|
262 |
|
|
//Creates a GET string (including the ?) that should be appended to a URL to
|
263 |
|
|
//pass the date and time.
|
264 |
|
|
//
|
265 |
|
|
function STRFUNC_datetime_get_url_trailer($sddt, $sdtim)
|
266 |
|
|
{
|
267 |
|
|
if (($sddt === FALSE) && ($sdtim === FALSE))
|
268 |
|
|
{
|
269 |
|
|
//Nothing to append.
|
270 |
|
|
return("");
|
271 |
|
|
}
|
272 |
|
|
else if (($sddt === FALSE) && ($sdtim !== FALSE))
|
273 |
|
|
{
|
274 |
|
|
//Time only.
|
275 |
|
|
return("?sdtim=" . $sdtim);
|
276 |
|
|
}
|
277 |
|
|
else if (($sddt !== FALSE) && ($sdtim === FALSE))
|
278 |
|
|
{
|
279 |
|
|
//Date only.
|
280 |
|
|
return("?sddt=" . $sddt);
|
281 |
|
|
}
|
282 |
|
|
else
|
283 |
|
|
{
|
284 |
|
|
//Both date and time.
|
285 |
|
|
return("?sddt=" . $sddt . "&sdtim=" . $sdtim);
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
?>
|