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

Annotation of /to_be_filed/webprojs/php_libraries/php_library/pamc/strfunc.inc

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25