Hi everyone,

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:

bool str_type(str subject, int type)

type is a bitmask created from a bunch of new constants (STR_TYPE_ALNUM, 
STR_TYPE_ALPHA, STR_TYPE_CNTRL, STR_TYPE_DIGIT, STR_TYPE_GRAPH, 
STR_TYPE_LOWER, STR_TYPE_PRINT, STR_TYPE_PUNCT, STR_TYPE_SPACE, 
STR_TYPE_UPPER, STR_TYPE_XDIGIT)

If anyone is interested, there's a more exotic version at 
http://www.coeus-group.com/en/uploads/php_str_type.2.diff which allows as 
many parameters as you want, each of which can be either one of the above 
constants or a bitmask. It's not as clean as the attached version, and the 
attached version is definitely more similar to other functions, but I thought 
this way would be nice for people who are intimidated by bitmasks.

If the attachment doesn't agree with anyone, it's also at 
http://www.coeus-group.com/en/uploads/php_str_type.1.diff


-- 
Evan Nemerson
[EMAIL PROTECTED]

--
"The greatest mistake is to imagine that the human being is an autonomous 
individual. The secret freedom which you can supposedly enjoy under a 
despotic government is nonsense, because your thoughts are never entirely 
your own. Philosophers, writers, artists, even scientists, not only need 
encouragement and an audience, they need constant stimulation from other 
people. It is almost impossible to think without talking. If Defoe had really 
lived on a desert island, he could not have written Robinson Crusoe, nor 
would he have wanted to. Take away freedom of speech, and the creative 
faculties dry up."

-George Orwell

Index: ext/standard/basic_functions.c
===================================================================
RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.642
diff -u -r1.642 basic_functions.c
--- ext/standard/basic_functions.c	19 Nov 2003 21:10:29 -0000	1.642
+++ ext/standard/basic_functions.c	21 Nov 2003 11:54:53 -0000
@@ -378,6 +378,7 @@
 	PHP_FE(str_split,														NULL)
 	PHP_FE(strpbrk,															NULL)
 	PHP_FE(substr_compare,														NULL)
+	PHP_FE(str_type,														NULL)
 
 #ifdef HAVE_STRCOLL
 	PHP_FE(strcoll,															NULL)
Index: ext/standard/string.c
===================================================================
RCS file: /repository/php-src/ext/standard/string.c,v
retrieving revision 1.400
diff -u -r1.400 string.c
--- ext/standard/string.c	30 Oct 2003 00:49:33 -0000	1.400
+++ ext/standard/string.c	21 Nov 2003 11:55:04 -0000
@@ -23,6 +23,7 @@
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
 #include <stdio.h>
+#include <ctype.h>
 #include "php.h"
 #include "reg.h"
 #include "php_rand.h"
@@ -64,6 +65,19 @@
 #define STR_STRSPN				0
 #define STR_STRCSPN				1
 
+#define STR_TYPE_ALNUM 0x01
+#define STR_TYPE_ALPHA 0x02
+#define STR_TYPE_CNTRL 0x04
+#define STR_TYPE_DIGIT 0x08
+#define STR_TYPE_GRAPH 0x10
+#define STR_TYPE_LOWER 0x20
+#define STR_TYPE_PRINT 0x40
+#define STR_TYPE_PUNCT 0x80
+#define STR_TYPE_SPACE 0x100
+#define STR_TYPE_UPPER 0x200
+#define STR_TYPE_XDIGIT 0x400
+
+
 /* {{{ register_string_constants
  */
 void register_string_constants(INIT_FUNC_ARGS)
@@ -97,6 +111,18 @@
 	REGISTER_LONG_CONSTANT("LC_MESSAGES", LC_MESSAGES, CONST_CS | CONST_PERSISTENT);
 # endif
 #endif
+
+	REGISTER_LONG_CONSTANT("STR_TYPE_ALNUM", STR_TYPE_ALNUM, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_ALPHA", STR_TYPE_ALPHA, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_CNTRL", STR_TYPE_CNTRL, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_DIGIT", STR_TYPE_DIGIT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_GRAPH", STR_TYPE_GRAPH, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_LOWER", STR_TYPE_LOWER, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_PRINT", STR_TYPE_PRINT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_PUNCT", STR_TYPE_PUNCT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_SPACE", STR_TYPE_SPACE, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_UPPER", STR_TYPE_UPPER, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("STR_TYPE_XDIGIT", STR_TYPE_XDIGIT, CONST_CS | CONST_PERSISTENT);
 	
 }
 /* }}} */
@@ -4691,6 +4717,50 @@
 }
 /* }}} */
 
+/* {{{ proto bool str_type(string str, int type)
+   Find out if a string is composed entirely of the specified type(s) of characters. */
+PHP_FUNCTION(str_type)
+{
+	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 & STR_TYPE_ALNUM) && (isalnum(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_ALPHA) && (isalpha(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_CNTRL) && (iscntrl(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_DIGIT) && (isdigit(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_GRAPH) && (isgraph(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_LOWER) && (islower(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_PRINT) && (isprint(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_PUNCT) && (ispunct(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_SPACE) && (isspace(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_UPPER) && (isupper(str[count])) )
+			res = 0;
+		else if ( (types & STR_TYPE_XDIGIT) && (isxdigit(str[count])) )
+			res = 0;
+
+		if ( res == 1 ) {
+			RETURN_FALSE;
+		}
+	}
+	RETURN_TRUE;
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
Index: ext/standard/php_string.h
===================================================================
RCS file: /repository/php-src/ext/standard/php_string.h,v
retrieving revision 1.80
diff -u -r1.80 php_string.h
--- ext/standard/php_string.h	30 Oct 2003 00:49:33 -0000	1.80
+++ ext/standard/php_string.h	21 Nov 2003 11:55:04 -0000
@@ -90,6 +90,7 @@
 PHP_FUNCTION(str_split);
 PHP_FUNCTION(strpbrk);
 PHP_FUNCTION(substr_compare);
+PHP_FUNCTION(str_type);
 #ifdef HAVE_STRCOLL
 PHP_FUNCTION(strcoll);
 #endif

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

Reply via email to