]> granicus.if.org Git - php/commitdiff
Added preg_quote() function.
authorAndrey Hristov <andrey@php.net>
Fri, 4 Jun 1999 13:56:23 +0000 (13:56 +0000)
committerAndrey Hristov <andrey@php.net>
Fri, 4 Jun 1999 13:56:23 +0000 (13:56 +0000)
ext/pcre/pcre.c
ext/pcre/php_pcre.h

index f813681f59f89babc88c081dad5984539c14ec86..958355b219c042fb1b028aff8af3b52987eaea8b 100644 (file)
@@ -47,6 +47,7 @@ function_entry pcre_functions[] = {
        PHP_FE(preg_match_all,  third_arg_force_ref)
        PHP_FE(preg_replace,    NULL)
        PHP_FE(preg_split,              NULL)
+       PHP_FE(preg_quote,              NULL)
        {NULL,          NULL,           NULL}
 };
 
@@ -843,6 +844,70 @@ PHP_FUNCTION(preg_split)
 /* }}} */
 
 
+/* {{{ proto string preg_quote(string str) */
+PHP_FUNCTION(preg_quote)
+{
+       zval    *in_str_arg;    /* Input string argument */
+       char    *in_str,                /* Input string */
+                       *out_str,               /* Output string with quoted characters */
+                       *p,                             /* Iterator for input string */
+                       *q,                             /* Iterator for output string */
+                        c;                             /* Current character */
+       
+       /* Get the arguments and check for errors */
+       if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &in_str_arg) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       /* Make sure we're working with strings */
+       convert_to_string(in_str_arg);
+       in_str = in_str_arg->value.str.val;
+
+       /* Nothing to do if we got an empty string */
+       if (!*in_str) {
+               RETVAL_STRING(empty_string, 0);
+       }
+       
+       /* Allocate enough memory so that even if each character
+          is quoted, we won't run out of room */
+       out_str = emalloc(2 * in_str_arg->value.str.len + 1);
+       
+       /* Go through the string and quote necessary characters */
+       for(p = in_str, q = out_str; (c = *p); p++) {
+               switch(c) {
+                       case '.':
+                       case '\\':
+                       case '+':
+                       case '*':
+                       case '?':
+                       case '[':
+                       case '^':
+                       case ']':
+                       case '$':
+                       case '(':
+                       case ')':
+                       case '{':
+                       case '}':
+                       case '=':
+                       case '!':
+                       case '>':
+                       case '<':
+                       case '|':
+                       case ':':
+                               *q++ = '\\';
+                               /* break is missing _intentionally_ */
+                       default:
+                               *q++ = c;
+               }
+       }
+       *q = '\0';
+       
+       /* Reallocate string and return it */
+       RETVAL_STRING(erealloc(out_str, q - out_str + 1), 0);
+}
+/* }}} */
+
+
 #endif /* HAVE_PCRE */
 
 /*
index 99f7578ca9cd7789e4bc557c1b03730047a2fdb0..5c7fbbbeb5f524b33d138b2d64a26056b3e53918 100644 (file)
@@ -45,6 +45,7 @@ PHP_FUNCTION(preg_match);
 PHP_FUNCTION(preg_match_all);
 PHP_FUNCTION(preg_replace);
 PHP_FUNCTION(preg_split);
+PHP_FUNCTION(preg_quote);
 
 extern zend_module_entry pcre_module_entry;
 #define pcre_module_ptr &pcre_module_entry