On Friday 21 November 2003 01:16 pm, you wrote:
> > I've attached a patch which adds a function that basically wraps the
>
> ctype.h
>
> > functions. It pretty much just lets you check to see if a string contains
> > only a certain set of characters. I think it would be very nice for input
> > validation, particularly for those who fear regular expressions. Here's
>
> the
>
> > prototype:
>
> A couple things:
>
> #1  Have you looked at the ctype extension?
> http://us4.php.net/manual/en/ref.ctype.php

Yeah, but I like being able to use them all at once.

>
> #2 If a unified function is to be added (not against it per se) it should
> probably be named ctype() (as the "top-level" of the ctype extension and be
> placed in ext/ctype rather than in ext/standard/strings.[ch]

Agreed. Here's a new patch that does that, and renames the calls the constants
CTYPE_*

>
> -Sara

-- 
Evan Nemerson
[EMAIL PROTECTED]


Index: ext/ctype/ctype.c
===================================================================
RCS file: /repository/php-src/ext/ctype/ctype.c,v
retrieving revision 1.28
diff -u -r1.28 ctype.c
--- ext/ctype/ctype.c	3 Oct 2003 15:50:01 -0000	1.28
+++ ext/ctype/ctype.c	21 Nov 2003 22:10:36 -0000
@@ -55,17 +55,49 @@
 	PHP_FE(ctype_space,	NULL)
 	PHP_FE(ctype_upper,	NULL)
 	PHP_FE(ctype_xdigit,	NULL)
+	PHP_FE(ctype,   	NULL)
 	{NULL, NULL, NULL}	/* Must be the last line in ctype_functions[] */
 };
 /* }}} */
 
+#define CTYPE_ALNUM 0x01
+#define CTYPE_ALPHA 0x02
+#define CTYPE_CNTRL 0x04
+#define CTYPE_DIGIT 0x08
+#define CTYPE_GRAPH 0x10
+#define CTYPE_LOWER 0x20
+#define CTYPE_PRINT 0x40
+#define CTYPE_PUNCT 0x80
+#define CTYPE_SPACE 0x100
+#define CTYPE_UPPER 0x200
+#define CTYPE_XDIGIT 0x400
+
+/* {{{ PHP_MINIT_FUNCTION
+ */
+PHP_MINIT_FUNCTION(ctype)
+{
+	REGISTER_LONG_CONSTANT("CTYPE_ALNUM", CTYPE_ALNUM, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_ALPHA", CTYPE_ALPHA, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_CNTRL", CTYPE_CNTRL, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_DIGIT", CTYPE_DIGIT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_GRAPH", CTYPE_GRAPH, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_LOWER", CTYPE_LOWER, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_PRINT", CTYPE_PRINT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_PUNCT", CTYPE_PUNCT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_SPACE", CTYPE_SPACE, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_UPPER", CTYPE_UPPER, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("CTYPE_XDIGIT", CTYPE_XDIGIT, CONST_CS | CONST_PERSISTENT);
+	return SUCCESS;
+}
+/* }}} */
+
 /* {{{ ctype_module_entry
  */
 zend_module_entry ctype_module_entry = {
 	STANDARD_MODULE_HEADER,
 	"ctype",
 	ctype_functions,
-	NULL,
+	PHP_MINIT(ctype),
 	NULL,
 	NULL,
 	NULL,
@@ -204,6 +236,50 @@
 }
 /* }}} */
 
+/* {{{ proto bool ctype(string str, int type)
+   Find out if a string is composed entirely of the specified type(s) of characters. */
+PHP_FUNCTION(ctype)
+{
+	char *str;
+	int str_l, types, count, res;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &str, &str_l, &types) == FAILURE) {
+		RETURN_FALSE;
+	}
+
+	for ( count=0 ; count<str_l ; count++ ) {
+		res = 1;
+		if ( (types & CTYPE_ALNUM) && (isalnum(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_ALPHA) && (isalpha(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_CNTRL) && (iscntrl(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_DIGIT) && (isdigit(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_GRAPH) && (isgraph(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_LOWER) && (islower(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_PRINT) && (isprint(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_PUNCT) && (ispunct(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_SPACE) && (isspace(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_UPPER) && (isupper(str[count])) )
+			res = 0;
+		else if ( (types & CTYPE_XDIGIT) && (isxdigit(str[count])) )
+			res = 0;
+
+		if ( res == 1 ) {
+			RETURN_FALSE;
+		}
+	}
+	RETURN_TRUE;
+}
+/* }}} */
+
 #endif	/* HAVE_CTYPE */
 
 /*
Index: ext/ctype/php_ctype.h
===================================================================
RCS file: /repository/php-src/ext/ctype/php_ctype.h,v
retrieving revision 1.10
diff -u -r1.10 php_ctype.h
--- ext/ctype/php_ctype.h	10 Jun 2003 20:03:26 -0000	1.10
+++ ext/ctype/php_ctype.h	21 Nov 2003 22:10:36 -0000
@@ -50,6 +50,7 @@
 PHP_FUNCTION(ctype_space);
 PHP_FUNCTION(ctype_upper);
 PHP_FUNCTION(ctype_xdigit);
+PHP_FUNCTION(ctype);
 
 /* 
   	Declare any global variables you may need between the BEGIN

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to