1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/pamc/gen_a/sw/phplib/string/strfunc_charset.inc,v 1.1 2007/06/21 23:34:03 dashley Exp $
|
3 |
//--------------------------------------------------------------------------------
|
4 |
//str_charset.inc -- PAMC string functions related to sets of characters.
|
5 |
//Copyright (C) 2007 David T. Ashley
|
6 |
//--------------------------------------------------------------------------------
|
7 |
//This program is free software; you can redistribute it and/or
|
8 |
//modify it under the terms of the GNU General Public License
|
9 |
//as published by the Free Software Foundation; either version 2
|
10 |
//of the License, or (at your option) any later version.
|
11 |
//
|
12 |
//This program is distributed in the hope that it will be useful,
|
13 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
//GNU General Public License for more details.
|
16 |
//
|
17 |
//You should have received a copy of the GNU General Public License
|
18 |
//along with this program; if not, write to the Free Software
|
19 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
//--------------------------------------------------------------------------------
|
21 |
//Author contact information:
|
22 |
// David T. Ashley
|
23 |
// P.O. Box 918
|
24 |
// Marshall, Michigan, 49068
|
25 |
// dta@e3ft.com
|
26 |
//--------------------------------------------------------------------------------
|
27 |
//Returns 1 if the string is purely digits '0'-'9', or 0 otherwise.
|
28 |
//
|
29 |
//Unit tested 20060408.
|
30 |
//
|
31 |
function STRFUNC_is_pure_digits($arg)
|
32 |
{
|
33 |
//Must be a string.
|
34 |
if (! is_string($arg))
|
35 |
return(0);
|
36 |
|
37 |
$len = strlen($arg);
|
38 |
|
39 |
for ($i=0; $i<$len; $i++)
|
40 |
{
|
41 |
$c = SubStr($arg, $i, 1);
|
42 |
if (strpos("0123456789", $c) === FALSE)
|
43 |
return(0);
|
44 |
}
|
45 |
|
46 |
return(1);
|
47 |
}
|
48 |
//
|
49 |
//
|
50 |
//--------------------------------------------------------------------------------
|
51 |
//Returns 1 if each character in the subset is present in the set, i.e.
|
52 |
//if it is a proper or improper subset, or 0 otherwise.
|
53 |
//
|
54 |
//Unit tested 20060408.
|
55 |
//
|
56 |
function STRFUNC_is_char_subset($conjectured_subset, $reference_set)
|
57 |
{
|
58 |
$conjsubsetlen = strlen($conjectured_subset);
|
59 |
$refsetlen = strlen($reference_set);
|
60 |
|
61 |
for ($i=0; $i<$conjsubsetlen; $i++)
|
62 |
{
|
63 |
$c = SubStr($conjectured_subset, $i, 1);
|
64 |
|
65 |
if (strpos($reference_set, $c) === FALSE)
|
66 |
return(0);
|
67 |
}
|
68 |
|
69 |
//If we've made it this far, no character has failed to pan out.
|
70 |
return(1);
|
71 |
}
|
72 |
//--------------------------------------------------------------------------------
|
73 |
//Removes all characters from the input that are not in the set of
|
74 |
//allowed characters.
|
75 |
//
|
76 |
function STRFUNC_force_into_subset($input, $subset)
|
77 |
{
|
78 |
$inputlen = strlen($input);
|
79 |
$subsetlen = strlen($subset);
|
80 |
|
81 |
$rv = "";
|
82 |
for ($i=0; $i<$inputlen; $i++)
|
83 |
{
|
84 |
$c = SubStr($input, $i, 1);
|
85 |
|
86 |
if (strpos($subset, $c) === FALSE)
|
87 |
{
|
88 |
//Character is not in the set. Do not add it to
|
89 |
//the result.
|
90 |
}
|
91 |
else
|
92 |
{
|
93 |
//Character is in the set. Add it.
|
94 |
$rv .= $c;
|
95 |
}
|
96 |
}
|
97 |
|
98 |
return($rv);
|
99 |
}
|
100 |
//
|
101 |
//--------------------------------------------------------------------------------
|
102 |
//Forces the variable to be a string, removes all characters from the input that
|
103 |
//are not in the set of allowed characters, then truncates the string if it is
|
104 |
//too long.
|
105 |
//
|
106 |
function STRFUNC_force_stringtype_subset_truncate($input, $subset, $maxlen)
|
107 |
{
|
108 |
//Force the type. Only numerics and strings can reliably be
|
109 |
//strings.
|
110 |
//
|
111 |
if (is_string($input))
|
112 |
{
|
113 |
//It is already a string. Do nothing.
|
114 |
}
|
115 |
else if (is_numeric($input))
|
116 |
{
|
117 |
//A number can be reliably made to a string.
|
118 |
$input = (string) $input;
|
119 |
}
|
120 |
else
|
121 |
{
|
122 |
//We don't know what it is. Make the empty string out of it.
|
123 |
$input = (string) "";
|
124 |
}
|
125 |
|
126 |
//Force it into the allowed character set.
|
127 |
$input = STRFUNC_force_into_subset($input, $subset);
|
128 |
|
129 |
//Take care of the length.
|
130 |
if (strlen($input) > $maxlen)
|
131 |
$input = SubStr($input, 0, $maxlen);
|
132 |
|
133 |
//echo " / " . $input . " / ";
|
134 |
|
135 |
return($input);
|
136 |
}
|
137 |
//
|
138 |
//--------------------------------------------------------------------------------
|
139 |
//End of $RCSfile: strfunc_charset.inc,v $.
|
140 |
//--------------------------------------------------------------------------------
|
141 |
?>
|