]> granicus.if.org Git - php/commitdiff
Added htmlspecialchars_decode() function for fast conversion from
authorIlia Alshanetsky <iliaa@php.net>
Mon, 7 Mar 2005 19:37:27 +0000 (19:37 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Mon, 7 Mar 2005 19:37:27 +0000 (19:37 +0000)
htmlspecialchars() generated entities back to characters.

NEWS
ext/standard/basic_functions.c
ext/standard/html.c
ext/standard/html.h

diff --git a/NEWS b/NEWS
index 83397e4a7fa6fe968e9a293168d272f56512a175..97c9794ee52e43f02c89c4b29538345282432eb1 100644 (file)
--- 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)
index 1f3987b1c8efcaa7723d8c4071901a24e8c616c7..e302202bdc0ff0139182d126405a368f6ba19fa4 100644 (file)
@@ -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)
index e1ac9188bc74624bd1aa2e700ba1afcd900803c7..fa50b5928d233521c518d0065ba867b0ee761594 100644 (file)
@@ -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, &quote_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, "&amp;", sizeof("&amp;") - 1, e))) {
+               int e_len = sizeof("&amp;") - 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)
index e2f7c7699447ce4e2ea3c4b323dda162947304b4..7fb91189c68c0895adeb818699f9480df4da6a45 100644 (file)
@@ -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);