diff options
-rw-r--r-- | ext/pcre/pcre.c | 65 | ||||
-rw-r--r-- | ext/pcre/php_pcre.h | 1 |
2 files changed, 66 insertions, 0 deletions
diff --git a/ext/pcre/pcre.c b/ext/pcre/pcre.c index f813681f59..958355b219 100644 --- a/ext/pcre/pcre.c +++ b/ext/pcre/pcre.c @@ -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 */ /* diff --git a/ext/pcre/php_pcre.h b/ext/pcre/php_pcre.h index 99f7578ca9..5c7fbbbeb5 100644 --- a/ext/pcre/php_pcre.h +++ b/ext/pcre/php_pcre.h @@ -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 |