From: Ilia Alshanetsky Date: Mon, 7 Mar 2005 19:37:27 +0000 (+0000) Subject: Added htmlspecialchars_decode() function for fast conversion from X-Git-Tag: RELEASE_0_3~43 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=975ff6f5d56bab8898bcdef1dddd0fb795a60619;p=php Added htmlspecialchars_decode() function for fast conversion from htmlspecialchars() generated entities back to characters. --- diff --git a/NEWS b/NEWS index 83397e4a7f..97c9794ee5 100644 --- a/NEWS +++ b/NEWS @@ -53,6 +53,7 @@ PHP NEWS . inet_ntop() (Sara) . fputcsv() (David Sklar) . posix_access() (Magnus) + . htmlspecialchars_decode() (Ilia) - Added DomDocument::$recover property for parsing not well-formed XML Documents. (Christian) - Added Cursor support for MySQL 5.0.x in mysqli (Georg) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 1f3987b1c8..e302202bdc 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -192,6 +192,7 @@ function_entry basic_functions[] = { PHP_FE(htmlspecialchars, NULL) PHP_FE(htmlentities, NULL) PHP_FE(html_entity_decode, NULL) + PHP_FE(htmlspecialchars_decode, NULL) PHP_FE(get_html_translation_table, NULL) PHP_FE(sha1, NULL) PHP_FE(sha1_file, NULL) diff --git a/ext/standard/html.c b/ext/standard/html.c index e1ac9188bc..fa50b5928d 100644 --- a/ext/standard/html.c +++ b/ext/standard/html.c @@ -1206,6 +1206,57 @@ PHP_FUNCTION(htmlspecialchars) } /* }}} */ +/* {{{ proto string htmlspecialchars(string string [, int quote_style]) + Convert special HTML entities back to characters */ +PHP_FUNCTION(htmlspecialchars_decode) +{ + char *str, *new_str, *e, *p; + int len, i, new_len; + long quote_style = ENT_COMPAT; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &len, "e_style) == FAILURE) { + return; + } + + new_str = estrndup(str, len); + new_len = len; + + for (i = 0; basic_entities[i].charcode != 0; i++) { + if (basic_entities[i].flags && !(quote_style & basic_entities[i].flags)) { + continue; + } + + e = new_str + new_len; + p = new_str; + + while ((p = php_memnstr(p, basic_entities[i].entity, basic_entities[i].entitylen, e))) { + int e_len = basic_entities[i].entitylen - 1; + + *p++ = basic_entities[i].charcode; + memmove(p, p + e_len, (e - p - e_len)); + + new_len -= e_len; + e -= e_len; + } + } + + e = new_str + new_len; + p = new_str; + while ((p = php_memnstr(p, "&", sizeof("&") - 1, e))) { + int e_len = sizeof("&") - 2; + + p++; + memmove(p, p + e_len, (e - p - e_len)); + + new_len -= e_len; + e -= e_len; + } + + new_str[new_len] = '\0'; + RETURN_STRINGL(new_str, new_len, 0); +} +/* }}} */ + /* {{{ proto string html_entity_decode(string string [, int quote_style][, string charset]) Convert all HTML entities to their applicable characters */ PHP_FUNCTION(html_entity_decode) diff --git a/ext/standard/html.h b/ext/standard/html.h index e2f7c76994..7fb91189c6 100644 --- a/ext/standard/html.h +++ b/ext/standard/html.h @@ -33,6 +33,7 @@ void register_html_constants(INIT_FUNC_ARGS); PHP_FUNCTION(htmlspecialchars); PHP_FUNCTION(htmlentities); +PHP_FUNCTION(htmlspecialchars_decode); PHP_FUNCTION(html_entity_decode); PHP_FUNCTION(get_html_translation_table);