2 if ( ! defined( 'MEDIAWIKI' ) )
5 * PHP <4.2.0 doesn't build ctype_ functions by default and Gentoo doesn't
6 * build it by default, and that probably applies for some other distributions
8 * These functions should be fully compatable with their PHP equivalents
11 * @subpackage Compatability
13 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
14 * @copyright Copyright © 2006, Ævar Arnfjörð Bjarmason
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
17 * @link http://www.php.net/manual/en/ref.ctype.php PHP Character Type Functions
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 * Takes the same input and returns the same values as the equvalent PHP
41 function ctype_alnum() {
42 $fname = 'ctype_alnum';
44 $args = func_get_args();
45 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
47 if ( ! is_null( $ret ) )
50 return (bool)preg_match( '~^[[:alnum:]]+$~i', $in );
53 function ctype_alpha() {
54 $fname = 'ctype_alpha';
56 $args = func_get_args();
57 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
59 if ( ! is_null( $ret ) )
62 return (bool)preg_match( '~^[[:alpha:]]+$~i', $in );
65 function ctype_cntrl() {
66 $fname = 'ctype_cntrl';
68 $args = func_get_args();
69 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
71 if ( ! is_null( $ret ) )
74 return (bool)preg_match( '~^[[:cntrl:]]+$~', $in );
77 function ctype_digit() {
78 $fname = 'ctype_digit';
80 $args = func_get_args();
81 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
83 if ( ! is_null( $ret ) )
86 return (bool)preg_match( '~^[[:digit:]]+$~', $in );
89 function ctype_graph() {
90 $fname = 'ctype_graph';
92 $args = func_get_args();
93 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
95 if ( ! is_null( $ret ) )
98 return (bool)preg_match( '~^[[:graph:]]+$~', $in );
101 function ctype_lower() {
102 $fname = 'ctype_lower';
104 $args = func_get_args();
105 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
107 if ( ! is_null( $ret ) )
110 return (bool)preg_match( '~^[[:lower:]]+$~', $in );
113 function ctype_print() {
114 $fname = 'ctype_print';
116 $args = func_get_args();
117 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
119 if ( ! is_null( $ret ) )
122 return (bool)preg_match( '~^[[:print:]]+$~', $in );
125 function ctype_punct() {
126 $fname = 'ctype_punct';
128 $args = func_get_args();
129 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
131 if ( ! is_null( $ret ) )
134 return (bool)preg_match( '~^[[:punct:]]+$~', $in );
137 function ctype_space() {
138 $fname = 'ctype_space';
140 $args = func_get_args();
141 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
143 if ( ! is_null( $ret ) )
146 return (bool)preg_match( '~^[[:space:]]+$~', $in );
151 function ctype_upper() {
152 $fname = 'ctype_upper';
154 $args = func_get_args();
155 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
157 if ( ! is_null( $ret ) )
160 return (bool)preg_match( '~^[[:upper:]]+$~', $in );
164 function ctype_xdigit() {
165 $fname = 'ctype_xdigit';
167 $args = func_get_args();
168 list( $in, $ret ) = wf_ctype_parse_args( $fname, $args );
170 if ( ! is_null( $ret ) )
173 return (bool)preg_match( '~^[[:xdigit:]]+$~i', $in );
179 * PHP does some munging on ctype_*() like converting -128 <= x <= -1 to x +=
180 * 256, 0 <= x <= 255 to chr(x) etc, it'll return true if x < -128 and false if
181 * x >= 256, true if the input is an empty string and false if it's of any
182 * other datatype than int or string, this function duplicates that behaviour.
184 * @param string $fname The name of the caller function
185 * @param array $args The return of the callers func_get_args()
186 * @return array An array with two items, the first is the munged input the
187 * calling function is supposed to use and a return value it
188 * should return if it's not null, $in will be null if $ret is
189 * not null since there's no point in setting it in that case
191 function wf_ctype_parse_args( $fname, $args ) {
194 $cnt = count( $args );
197 trigger_error( "$fname() expects exactly 1 parameter $cnt given", E_USER_WARNING
);
199 $in = array_pop( $args );
201 if ( is_int( $in ) ) {
203 return array( null, false );
206 else if ( $in >= -128 )
207 $in = ord( $in +
256 );
208 else if ( $in < -128 )
209 return array( null, true );
212 if ( is_string( $in ) )
214 return array( null, true );
216 return array( $in, null );
219 return array( null, false );