Re: [問題] JS字串包含有""等符號及斷行 及 傳值的 …

看板Web_Design作者 (愚人)時間18年前 (2007/10/07 12:35), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《boy777 (missing each other)》之銘言: : http://www.htaccesseditor.com/tc.shtml : 想做出類似這樣的網站 : 使用者輸入資料 之後另存為新的HTML檔 : 但是字串中包含了大量的""和斷行 : 我知道"要再前方加入倒斜線即可 但是斷行該如何是好 : 只要遇到斷行 就無法順利輸出文字 : 求救@@ 謝謝大家 取自 commons-lang StringEscapeUtils http://commons.apache.org/downloads/download_lang.cgi 雖然這是 java code 不過你改 下差不多就能用了 ================================================================== private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException { if (out == null) { throw new IllegalArgumentException("The Writer must not be null"); } if (str == null) { return; } int sz; sz = str.length(); for (int i = 0; i < sz; i++) { char ch = str.charAt(i); // handle unicode if (ch > 0xfff) { out.write("\\u" + hex(ch)); } else if (ch > 0xff) { out.write("\\u0" + hex(ch)); } else if (ch > 0x7f) { out.write("\\u00" + hex(ch)); } else if (ch < 32) { switch (ch) { case '\b': out.write('\\'); out.write('b'); break; case '\n': out.write('\\'); out.write('n'); break; case '\t': out.write('\\'); out.write('t'); break; case '\f': out.write('\\'); out.write('f'); break; case '\r': out.write('\\'); out.write('r'); break; default : if (ch > 0xf) { out.write("\\u00" + hex(ch)); } else { out.write("\\u000" + hex(ch)); } break; } } else { switch (ch) { case '\'': if (escapeSingleQuote) { out.write('\\'); } out.write('\''); break; case '"': out.write('\\'); out.write('"'); break; case '\\': out.write('\\'); out.write('\\'); break; default : out.write(ch); break; } } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.231.49.182

10/07 20:34, , 1F
謝謝 這個我得看很久~~
10/07 20:34, 1F
文章代碼(AID): #17268X5K (Web_Design)
討論串 (同標題文章)
文章代碼(AID): #17268X5K (Web_Design)