/[dtapublic]/to_be_filed/webprojs/php_libraries/php_library/fboprime/strfunc.inc
ViewVC logotype

Contents of /to_be_filed/webprojs/php_libraries/php_library/fboprime/strfunc.inc

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25