]> granicus.if.org Git - php/commitdiff
@- Added second argument to preg_quote() which allows quoting of
authorAndrei Zmievski <andrei@php.net>
Thu, 25 May 2000 21:04:09 +0000 (21:04 +0000)
committerAndrei Zmievski <andrei@php.net>
Thu, 25 May 2000 21:04:09 +0000 (21:04 +0000)
@  one additional character, usually the regex delimiter. (Andrei)

ext/pcre/php_pcre.c

index d84c5ff744e1ac69d8228a3877acdad9844b5b9f..3467503cff9f2d8b017e4298d72db0b5c394dc48 100644 (file)
@@ -1036,15 +1036,19 @@ PHP_FUNCTION(preg_split)
 PHP_FUNCTION(preg_quote)
 {
        zval    **in_str_arg;   /* Input string argument */
+       zval    **delim;                /* Additional delimiter argument */
        char    *in_str,                /* Input string */
                *in_str_end,    /* End of the input string */
                        *out_str,               /* Output string with quoted characters */
                        *p,                             /* Iterator for input string */
                        *q,                             /* Iterator for output string */
+                        delim_char,    /* Delimiter character to be quoted */
                         c;                             /* Current character */
+       zend_bool quote_delim = 0; /* Whether to quote additional delim char */
        
        /* Get the arguments and check for errors */
-       if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &in_str_arg) == FAILURE) {
+       if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+               zend_get_parameters_ex(ARG_COUNT(ht), &in_str_arg, &delim) == FAILURE) {
                WRONG_PARAM_COUNT;
        }
        
@@ -1057,6 +1061,14 @@ PHP_FUNCTION(preg_quote)
        if (in_str == in_str_end) {
                RETVAL_STRINGL(empty_string, 0, 0);
        }
+
+       if (ARG_COUNT(ht) == 2) {
+               convert_to_string_ex(delim);
+               if (Z_STRLEN_PP(delim) > 0) {
+                       delim_char = Z_STRVAL_PP(delim)[0];
+                       quote_delim = 1;
+               }
+       }
        
        /* Allocate enough memory so that even if each character
           is quoted, we won't run out of room */
@@ -1086,9 +1098,14 @@ PHP_FUNCTION(preg_quote)
                        case '|':
                        case ':':
                                *q++ = '\\';
-                               /* break is missing _intentionally_ */
+                               *q++ = c;
+                               break;
+
                        default:
+                               if (c == delim_char && quote_delim)
+                                       *q++ = '\\';
                                *q++ = c;
+                               break;
                }
        }
        *q = '\0';