Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Escape HTML special characters from a String

Name: Anonymous 2010-01-08 10:27

public static final String escapeHTML(String s){
   StringBuffer sb = new StringBuffer();
   int n = s.length();
   for (int i = 0; i < n; i++) {
      char c = s.charAt(i);
      switch (c) {
         case '<': sb.append("&lt;"); break;
         case '>': sb.append("&gt;"); break;
         case '&': sb.append("&amp;"); break;
         case '"': sb.append("&quot;"); break;
         case 'à': sb.append("&agrave;");break;
         case 'À': sb.append("&Agrave;");break;
         case 'â': sb.append("&acirc;");break;
         case 'Â': sb.append("&Acirc;");break;
         case 'ä': sb.append("&auml;");break;
         case 'Ä': sb.append("&Auml;");break;
         case 'å': sb.append("&aring;");break;
         case 'Å': sb.append("&Aring;");break;
         case 'æ': sb.append("&aelig;");break;
         case 'Æ': sb.append("&AElig;");break;
         case 'ç': sb.append("&ccedil;");break;
         case 'Ç': sb.append("&Ccedil;");break;
         case 'é': sb.append("&eacute;");break;
         case 'É': sb.append("&Eacute;");break;
         case 'è': sb.append("&egrave;");break;
         case 'È': sb.append("&Egrave;");break;
         case 'ê': sb.append("&ecirc;");break;
         case 'Ê': sb.append("&Ecirc;");break;
         case 'ë': sb.append("&euml;");break;
         case 'Ë': sb.append("&Euml;");break;
         case 'ï': sb.append("&iuml;");break;
         case 'Ï': sb.append("&Iuml;");break;
         case 'ô': sb.append("&ocirc;");break;
         case 'Ô': sb.append("&Ocirc;");break;
         case 'ö': sb.append("&ouml;");break;
         case 'Ö': sb.append("&Ouml;");break;
         case 'ø': sb.append("&oslash;");break;
         case 'Ø': sb.append("&Oslash;");break;
         case 'ß': sb.append("&szlig;");break;
         case 'ù': sb.append("&ugrave;");break;
         case 'Ù': sb.append("&Ugrave;");break;        
         case 'û': sb.append("&ucirc;");break;        
         case 'Û': sb.append("&Ucirc;");break;
         case 'ü': sb.append("&uuml;");break;
         case 'Ü': sb.append("&Uuml;");break;
         case '®': sb.append("&reg;");break;        
         case '©': sb.append("&copy;");break;  
         case '€': sb.append("&euro;"); break;
         // be carefull with this one (non-breaking whitee space)
         case ' ': sb.append("&nbsp;");break;        
        
         default:  sb.append(c); break;
      }
   }
   return sb.toString();
}

Name: Anonymous 2010-01-08 10:31

Okay.

Name: Anonymous 2010-01-08 10:38

You just wanted to brag about being able to use switch instead of if-then-else? Well believe me, it's nothing special. I'm in second year and we're studying design patterns.

Name: Anonymous 2010-01-08 10:41

>>3
I'm in second year and we're studying fibs and hanois, and people don't understand them.

Name: Anonymous 2010-01-08 10:42

this is totally gay. a look up table using the character code as an index would be much more l333337xor

Name: Anonymous 2010-01-08 10:52

>>3
THE ``IF'' DESIGN PATTERN

Name: Anonymous 2010-01-08 10:59

ąęśżźćół
what are you going with this, eh?

Name: Anonymous 2010-01-08 11:05

>>7
sage because you clearly can't be bothered to learn English

Name: Anonymous 2010-01-08 11:09

>>8
oh, i was going to write something else but then changed my mind, deleted some text and wrote this - i guess i deleted too much. i'm very sorry, i had a weary day.

Name: Anonymous 2010-01-08 11:12

>>8
"GRUNNUR"

Name: Anonymous 2010-01-08 11:13

>>9
it's ok, forgiven.

Name: Anonymous 2010-01-08 11:31

Writing some code to write code for me:

(defparameter *character->html-encoding-table*
  '((#\< "&lt;")
    (#\> "&gt;")
    ;; insert other cases here, or write something clever
    ;; to generate them from a source file (& and ; parts)
    ))

(defun html-escape-string (string) 
  (apply #'concatenate 'string
    (map 'list
         #'(lambda (x)
             ;; or I could use something like KMP's meta, but whatever.
             #.`(case x
                  ,@*character->html-encoding-table*
                  (otherwise (string x))))
         string)))

;; Test
;CL-USER> (html-escape-string "abc><")
;"abc&gt;&lt;"

Name: Anonymous 2010-01-08 11:37

only escape stuff like < and > and utf8 the rest. &uuml; etc. is obsolete

Name: Anonymous 2010-01-08 12:22

>>13
utf8
Terrible!

Name: Anonymous 2010-01-08 16:39

>>13

I think you also have to escape &

However I am not an expert on the field of HTML Entities. I recommend you ask Hixie abut them.

Name: Anonymous 2010-01-08 16:57

sb.append

DON'T REPEAT YOURSELF!

Name: Anonymous 2010-01-08 17:02

>>16
    was I supposed to use
        |   sb.apend
?

Name: Anonymous 2010-01-08 17:09


PHPAPI char *php_escape_html_entities_ex(unsigned char *old, int oldlen, int *newlen, int all, int quote_style, char *hint_charset, zend_bool double_encode 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);
        int status = SUCCESS;
        unsigned int this_char = get_next_char(charset, old, oldlen, &i, mbsequence, &mbseqlen, &status);

        if(status == FAILURE) {
            /* invalid MB sequence */
            efree(replaced);
            if(!PG(display_errors)) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid multibyte sequence in argument");
            }
            *newlen = 0;
            return STR_EMPTY_ALLOC();
        }
        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++] = '&';
                strlcpy(replaced + len, rep, maxlen);
                len += l;
                replaced[len++] = ';';
            }
        }
        if (!matches_map) {   
            int is_basic = 0;

            if (this_char == '&') {
                if (double_encode) {
encode_amp:
                    memcpy(replaced + len, "&amp;", sizeof("&amp;") - 1);
                    len += sizeof("&amp;") - 1;
                } else {
                    char *e = memchr(old + i, ';', oldlen - i);
                    char *s = old + i;

                    if (!e || (e - s) > 10) { /* minor optimization to avoid "entities" over 10 chars in length */
                        goto encode_amp;
                    } else {
                        if (*s == '#') { /* numeric entities */
                            s++;
                            /* Hex (Z) */
                            if (*s == 'x' || *s == 'X') {
                                s++;
                                while (s < e) {
                                    if (!isxdigit((int)*(unsigned char *)s++)) {
                                        goto encode_amp;
                                    }
                                }
                            /* Dec (Z)*/
                            } else {
                                while (s < e) {
                                    if (!isdigit((int)*(unsigned char *)s++)) {
                                        goto encode_amp;
                                    }
                                }
                            }
                        } else { /* text entities */
                            while (s < e) {
                                if (!isalnum((int)*(unsigned char *)s++)) {
                                    goto encode_amp;
                                }
                            }
                        }
                        replaced[len++] = '&';
                    }
                }
                is_basic = 1;
            } else {
                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) {
                /* a wide char without a named entity; pass through the original sequence */
                if (mbseqlen > 1) {
                    memcpy(replaced + len, mbsequence, mbseqlen);
                    len += mbseqlen;
                } else {
                    replaced[len++] = (unsigned char)this_char;
                }
            }
        }
    }
    replaced[len] = '\0';
    *newlen = len;

    return replaced;


}

/* {{{ entity_charset determine_charset
 * returns the charset identifier based on current locale or a hint.
 * defaults to iso-8859-1 */
static enum entity_charset determine_charset(char *charset_hint TSRMLS_DC)
{
    int i;
    enum entity_charset charset = cs_8859_1;
    int len = 0;
    zval *uf_result = NULL;

    /* Guarantee default behaviour for backwards compatibility */
    if (charset_hint == NULL)
        return cs_8859_1;

    if ((len = strlen(charset_hint)) != 0) {
        goto det_charset;
    }
#if HAVE_MBSTRING
#if !defined(COMPILE_DL_MBSTRING)
    /* XXX: Ugly things. Why don't we look for a more sophisticated way? */
    switch (MBSTRG(current_internal_encoding)) {
        case mbfl_no_encoding_8859_1:
            return cs_8859_1;

        case mbfl_no_encoding_utf8:
            return cs_utf_8;

        case mbfl_no_encoding_euc_jp:
        case mbfl_no_encoding_eucjp_win:
            return cs_eucjp;

        case mbfl_no_encoding_sjis:
        case mbfl_no_encoding_sjis_win:
        case mbfl_no_encoding_sjis_mac:
            return cs_sjis;

        case mbfl_no_encoding_cp1252:
            return cs_cp1252;

        case mbfl_no_encoding_8859_15:
            return cs_8859_15;

        case mbfl_no_encoding_big5:
            return cs_big5;

        case mbfl_no_encoding_euc_cn:
        case mbfl_no_encoding_hz:
        case mbfl_no_encoding_cp936:
            return cs_gb2312;

        case mbfl_no_encoding_koi8r:
            return cs_koi8r;

        case mbfl_no_encoding_cp866:
            return cs_cp866;

        case mbfl_no_encoding_cp1251:
            return cs_cp1251;

        case mbfl_no_encoding_8859_5:
            return cs_8859_5;

        default:
            ;
    }
#else
    {
        zval nm_mb_internal_encoding;

        ZVAL_STRING(&nm_mb_internal_encoding, "mb_internal_encoding", 0);

        if (call_user_function_ex(CG(function_table), NULL, &nm_mb_internal_encoding, &uf_result, 0, NULL, 1, NULL TSRMLS_CC) != FAILURE) {

            charset_hint = Z_STRVAL_P(uf_result);
            len = Z_STRLEN_P(uf_result);
           
            if (len == 4) { /* sizeof(none|auto|pass)-1 */
                if (!memcmp("pass", charset_hint, sizeof("pass") - 1) ||
                    !memcmp("auto", charset_hint, sizeof("auto") - 1) ||
                    !memcmp("none", charset_hint, sizeof("none") - 1)) {
                   
                    charset_hint = NULL;
                    len = 0;
                }
            }
            goto det_charset;
        }
    }
#endif
#endif

    charset_hint = SG(default_charset);
    if (charset_hint != NULL && (len=strlen(charset_hint)) != 0) {
        goto det_charset;
    }

    /* 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);
        }
    }
#endif

det_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 (len == strlen(charset_map[i].codeset) && 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;
}
/* }}} */

Name: Anonymous 2010-10-27 2:58

Name: Anonymous 2011-02-03 6:42

Name: Anonymous 2011-02-17 20:03

check 'em dubz

Don't change these.
Name: Email:
Entire Thread Thread List