📄 html.c
字号:
} /* try to detect the charset for the locale */#if HAVE_NL_LANGINFO && HAVE_LOCALE_H && defined(CODESET) charset_hint = nl_langinfo(CODESET); if (charset_hint != NULL && (len=strlen(charset_hint)) != 0) { goto det_charset; }#endif#if HAVE_LOCALE_H /* try to figure out the charset from the locale */ { char *localename; char *dot, *at; /* lang[_territory][.codeset][@modifier] */ localename = setlocale(LC_CTYPE, NULL); dot = strchr(localename, '.'); if (dot) { dot++; /* locale specifies a codeset */ at = strchr(dot, '@'); if (at) len = at - dot; else len = strlen(dot); charset_hint = dot; } else { /* no explicit name; see if the name itself * is the charset */ charset_hint = localename; len = strlen(charset_hint); } }#endifdet_charset: if (charset_hint) { int found = 0; /* now walk the charset map and look for the codeset */ for (i = 0; charset_map[i].codeset; i++) { if (strncasecmp(charset_hint, charset_map[i].codeset, len) == 0) { charset = charset_map[i].charset; found = 1; break; } } if (!found) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "charset `%s' not supported, assuming iso-8859-1", charset_hint); } } if (uf_result != NULL) { zval_ptr_dtor(&uf_result); } return charset;}/* }}} *//* {{{ php_unescape_html_entities */PHPAPI char *php_unescape_html_entities(unsigned char *old, int oldlen, int *newlen, int all, int quote_style, char *hint_charset TSRMLS_DC){ int retlen; int j, k; char *replaced, *ret; enum entity_charset charset = determine_charset(hint_charset TSRMLS_CC); unsigned char replacement[15]; ret = estrndup(old, oldlen); retlen = oldlen; if (!retlen) { goto empty_source; } if (all) { /* look for a match in the maps for this charset */ for (j = 0; entity_map[j].charset != cs_terminator; j++) { if (entity_map[j].charset != charset) continue; for (k = entity_map[j].basechar; k <= entity_map[j].endchar; k++) { unsigned char entity[32]; int entity_length = 0; if (entity_map[j].table[k - entity_map[j].basechar] == NULL) continue; entity[0] = '&'; entity_length = strlen(entity_map[j].table[k - entity_map[j].basechar]); strncpy(&entity[1], entity_map[j].table[k - entity_map[j].basechar], sizeof(entity) - 2); entity[entity_length+1] = ';'; entity[entity_length+2] = '\0'; entity_length += 2; /* When we have MBCS entities in the tables above, this will need to handle it */ if (k > 0xff) { zend_error(E_WARNING, "cannot yet handle MBCS in html_entity_decode()!"); } replacement[0] = k; replacement[1] = '\0'; replaced = php_str_to_str(ret, retlen, entity, entity_length, replacement, 1, &retlen); efree(ret); ret = replaced; } } } for (j = 0; basic_entities[j].charcode != 0; j++) { if (basic_entities[j].flags && (quote_style & basic_entities[j].flags) == 0) continue; replacement[0] = (unsigned char)basic_entities[j].charcode; replacement[1] = '\0'; replaced = php_str_to_str(ret, retlen, basic_entities[j].entity, basic_entities[j].entitylen, replacement, 1, &retlen); efree(ret); ret = replaced; }empty_source: *newlen = retlen; return ret;}/* }}} *//* {{{ php_escape_html_entities */PHPAPI char *php_escape_html_entities(unsigned char *old, int oldlen, int *newlen, int all, int quote_style, char *hint_charset TSRMLS_DC){ int i, j, maxlen, len; char *replaced; enum entity_charset charset = determine_charset(hint_charset TSRMLS_CC); int matches_map; maxlen = 2 * oldlen; if (maxlen < 128) maxlen = 128; replaced = emalloc (maxlen); len = 0; i = 0; while (i < oldlen) { unsigned char mbsequence[16]; /* allow up to 15 characters in a multibyte sequence */ int mbseqlen = sizeof(mbsequence); unsigned short this_char = get_next_char(charset, old, &i, mbsequence, &mbseqlen); matches_map = 0; if (len + 16 > maxlen) replaced = erealloc (replaced, maxlen += 128); if (all) { /* look for a match in the maps for this charset */ unsigned char *rep = NULL; for (j = 0; entity_map[j].charset != cs_terminator; j++) { if (entity_map[j].charset == charset && this_char >= entity_map[j].basechar && this_char <= entity_map[j].endchar) { rep = (unsigned char*)entity_map[j].table[this_char - entity_map[j].basechar]; if (rep == NULL) { /* there is no entity for this position; fall through and * just output the character itself */ break; } matches_map = 1; break; } } if (matches_map) { int l = strlen(rep); /* increase the buffer size */ if (len + 2 + l >= maxlen) { replaced = erealloc(replaced, maxlen += 128); } replaced[len++] = '&'; strcpy(replaced + len, rep); len += l; replaced[len++] = ';'; } } if (!matches_map) { int is_basic = 0; for (j = 0; basic_entities[j].charcode != 0; j++) { if ((basic_entities[j].charcode != this_char) || (basic_entities[j].flags && (quote_style & basic_entities[j].flags) == 0)) continue; memcpy(replaced + len, basic_entities[j].entity, basic_entities[j].entitylen); len += basic_entities[j].entitylen; is_basic = 1; break; } if (!is_basic) { if (mbseqlen > 1) { /* a wide char without a named entity; pass through the original sequence */ memcpy(replaced + len, mbsequence, mbseqlen); len += mbseqlen; } else { replaced[len++] = (unsigned char)this_char; } } } } replaced[len] = '\0'; *newlen = len; return replaced;}/* }}} *//* {{{ php_html_entities */static void php_html_entities(INTERNAL_FUNCTION_PARAMETERS, int all){ char *str, *hint_charset = NULL; int str_len, hint_charset_len = 0; int len; long quote_style = ENT_COMPAT; char *replaced; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str, &str_len, "e_style, &hint_charset, &hint_charset_len) == FAILURE) { return; } replaced = php_escape_html_entities(str, str_len, &len, all, quote_style, hint_charset TSRMLS_CC); RETVAL_STRINGL(replaced, len, 0);}/* }}} */#define HTML_SPECIALCHARS 0#define HTML_ENTITIES 1/* {{{ register_html_constants */void register_html_constants(INIT_FUNC_ARGS){ REGISTER_LONG_CONSTANT("HTML_SPECIALCHARS", HTML_SPECIALCHARS, CONST_PERSISTENT|CONST_CS); REGISTER_LONG_CONSTANT("HTML_ENTITIES", HTML_ENTITIES, CONST_PERSISTENT|CONST_CS); REGISTER_LONG_CONSTANT("ENT_COMPAT", ENT_COMPAT, CONST_PERSISTENT|CONST_CS); REGISTER_LONG_CONSTANT("ENT_QUOTES", ENT_QUOTES, CONST_PERSISTENT|CONST_CS); REGISTER_LONG_CONSTANT("ENT_NOQUOTES", ENT_NOQUOTES, CONST_PERSISTENT|CONST_CS);}/* }}} *//* {{{ proto string htmlspecialchars(string string [, int quote_style][, string charset]) Convert special characters to HTML entities */PHP_FUNCTION(htmlspecialchars){ php_html_entities(INTERNAL_FUNCTION_PARAM_PASSTHRU, 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){ char *str, *hint_charset = NULL; int str_len, hint_charset_len, len; long quote_style = ENT_COMPAT; char *replaced; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls", &str, &str_len, "e_style, &hint_charset, &hint_charset_len) == FAILURE) { return; } replaced = php_unescape_html_entities(str, str_len, &len, 1, quote_style, hint_charset TSRMLS_CC); RETVAL_STRINGL(replaced, len, 0);}/* }}} *//* {{{ proto string htmlentities(string string [, int quote_style][, string charset]) Convert all applicable characters to HTML entities */PHP_FUNCTION(htmlentities){ php_html_entities(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} *//* {{{ proto array get_html_translation_table([int table [, int quote_style]]) Returns the internal translation table used by htmlspecialchars and htmlentities */PHP_FUNCTION(get_html_translation_table){ long which = HTML_SPECIALCHARS, quote_style = ENT_COMPAT; int i, j; char ind[2]; enum entity_charset charset = determine_charset(NULL TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &which, "e_style) == FAILURE) { return; } array_init(return_value); ind[1] = 0; switch (which) { case HTML_ENTITIES: for (j=0; entity_map[j].charset != cs_terminator; j++) { if (entity_map[j].charset != charset) continue; for (i = 0; i <= entity_map[j].endchar - entity_map[j].basechar; i++) { char buffer[16]; if (entity_map[j].table[i] == NULL) continue; /* what about wide chars here ?? */ ind[0] = i + entity_map[j].basechar; sprintf(buffer, "&%s;", entity_map[j].table[i]); add_assoc_string(return_value, ind, buffer, 1); } } /* break thru */ case HTML_SPECIALCHARS: for (j = 0; basic_entities[j].charcode != 0; j++) { if (basic_entities[j].flags && (quote_style & basic_entities[j].flags) == 0) continue; ind[0] = (unsigned char)basic_entities[j].charcode; add_assoc_string(return_value, ind, basic_entities[j].entity, 1); } break; }}/* }}} *//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -