• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            xiaoguozi's Blog
            Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習(xí)慣原本生活的人不容易改變,就算現(xiàn)狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無(wú)法預(yù)料,人們需要更細(xì)心的觀察別人,要隨時(shí)注意才能保護(hù)別人,因?yàn)樗麄兾幢刂雷约阂裁础ぁぁぁぁ?/span>

            在PHP中查找中文字符,有兩種方案。

            1、中文字符是gbk(gb2312)

            有兩種解決方法

            第一種:

            將PHP保存為ASCII編碼,然后使用strpos查找,如:

            strpos($curl_res, ‘哈哈’)

            第二種:

            將PHP保存為UTF-8無(wú)BOM編碼,然后轉(zhuǎn)換字符串編碼為UTF-8,再查找,如:

            $curl_res = mb_convert_encoding($curl_res, ‘utf-8′, ‘gbk’);

            mb_strpos($curl_res, ‘哈哈’);

            2、中文字符是UTF-8

            有兩種解決方法

            第一種:

            將PHP保存為UTF-8無(wú)BOM編碼,然后使用strpos查找,如:

            strpos($curl_res, ‘哈哈’)

            第二種:

            將PHP保存為ASCII編碼,然后轉(zhuǎn)換字符串編碼為gbk,再查找,如:

            $curl_res = mb_convert_encoding($curl_res, ‘gbk’, ‘utf-8′);

            mb_strpos($curl_res, ‘哈哈’);

            應(yīng)該可以看出一些規(guī)律,就是:函數(shù)中的中文字符串參數(shù)的編碼和PHP文件保存格式的編碼一致,在使用函數(shù)時(shí)要考慮到!


                 我生成的那個(gè)html文件被EmEditor認(rèn)為UTF-8 with Signature。而好用的那個(gè)html文件被EmEditor認(rèn)為UTF-8 without Signature.
                對(duì)于這兩種UTF-8格式的轉(zhuǎn)換,我查看了網(wǎng)上信息,點(diǎn)擊記事本,EmEditor等文本編輯器的另存為,當(dāng)選擇了UTF-8的編碼格式時(shí),Add a Unicode Signature(BOM)這個(gè)選項(xiàng)被激活,只要選擇上,我的文件就可以存為UTF-8 with Signature的格式。可是,問題就在于,我用java怎么讓我的文件直接生成為 UTF-8 with Signature的格式。
                開始上google搜索UTF-8 with Signature,BOM,Add a Unicode Signature等關(guān)鍵字。
            http://www.unicode.org/unicode/faq/utf_bom.html#BOM
            我大致了解了他們兩個(gè)的區(qū)別。
            Q: What is a BOM?

            A: A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plaintext files. Under some higher level protocols, use of a BOM may be mandatory (or prohibited) in the Unicode data stream defined in that protocol.
            http://mindprod.com/jgloss/bom.html
            BOM
            Byte Order Marks are special characters at the beginning of a Unicode file to indicate whether it is big or little endian, in other words does the high or low order byte come first. These codes also tell whether the encoding is 8, 16 or 32 bit. You can recognise Unicode files by their starting byte order marks, and by the way Unicode-16 files are half zeroes and Unicode-32 files are three-quarters zeros. Unicode Endian Markers
            Byte-order mark Description
            EF BB BF UTF-8
            FF FE UTF-16 aka UCS-2, little endian
            FE FF UTF-16 aka UCS-2, big endian
            00 00 FF FE UTF-32 aka UCS-4, little endian.
            00 00 FE FF UTF-32 aka UCS-4, big-endian.
            There are also variants of these encodings that have an implied endian marker.
            Unfortunately, often applications, even Javac.exe, choke on these byte order marks. Java Readers don't automatically filter them out. There is not much you can do but manually remove them.


            http://cache.baidu.com/c?word=java%2Cbom&url=http%3A//tgdem530%2Eblogchina%2Ecom/&b=0&a=1&user=baidu
            c、UTF的字節(jié)序和BOM
            UTF-8以字節(jié)為編碼單元,沒有字節(jié)序的問題。UTF-16以兩個(gè)字節(jié)為編碼單元,在解釋一個(gè)UTF-16文本前,首先要弄清楚每個(gè)編碼單元的字節(jié)序。 例如收到一個(gè)“奎”的Unicode編碼是594E,“乙”的Unicode編碼是4E59。如果我們收到UTF-16字節(jié)流“594E”,那么這是 “奎”還是“乙”?

            Unicode規(guī)范中推薦的標(biāo)記字節(jié)順序的方法是BOM。BOM不是“Bill Of Material”的BOM表,而是Byte Order Mark。BOM是一個(gè)有點(diǎn)小聰明的想法:

            在UCS編碼中有一個(gè)叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的編碼是FEFF。而FFFE在UCS中是不存在的字符,所以不應(yīng)該出現(xiàn)在實(shí)際傳輸中。UCS規(guī)范建議我們?cè)趥鬏斪止?jié)流前,先傳輸字符"ZERO WIDTH NO-BREAK SPACE"。

            這樣如果接收者收到FEFF,就表明這個(gè)字節(jié)流是Big-Endian的;如果收到FFFE,就表明這個(gè)字節(jié)流是Little-Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被稱作BOM。

            UTF-8不需要BOM來表明字節(jié)順序,但可以用BOM來表明編碼方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8編碼是EF BB BF(讀者可以用我們前面介紹的編碼方法驗(yàn)證一下)。所以如果接收者收到以EF BB BF開頭的字節(jié)流,就知道這是UTF-8編碼了。

            Windows就是使用BOM來標(biāo)記文本文件的編碼方式的。


            原來BOM是在文件的開始加了幾個(gè)字節(jié)作為標(biāo)記。有了這個(gè)標(biāo)記,一些協(xié)議和系統(tǒng)才能識(shí)別。好,看看怎么加上這寫字節(jié)。
            終于在這里找到了
            http://mindprod.com/jgloss/encoding.html 
            UTF-8 
            8-bit encoded Unicode. neé UTF8. Optional marker on front of file: EF BB BF for reading. Unfortunately, OutputStreamWriter does not automatically insert the marker on writing. Notepad can't read the file without this marker. Now the question is, how do you get that marker in there? You can't just emit the bytes EF BB BF since they will be encoded and changed. However, the solution is quite simple. prw.write( '\ufeff' ); at the head of the file. This will be encoded as EF BB BF.
            DataOutputStreams have a binary length count in front of each string. Endianness does not apply to 8-bit encodings. Java DataOutputStream and ObjectOutputStream uses a slight variant of kosher UTF-8. To aid with compatibility with C in JNI, the null byte '\u0000' is encoded in 2-byte format rather than 1-byte, so that the encoded strings never have embedded nulls. Only the 1-byte, 2-byte, and 3-byte formats are used. Supplementary characters, (above 0xffff), are represented in the form of surrogate pairs (a pair of encoded 16 bit characters in a special range), rather than directly encoding the character.
             
            prw.write( '\ufeff' );就是這個(gè)。
            于是我的代碼變?yōu)椋?br /> public void htmlWrite(String charsetName) {
                    try {
                        out = new BufferedWriter(new OutputStreamWriter(
                                    new FileOutputStream(outFileName), "UTF-8"));
                        out.write('\ufeff');
                        out.write(res);
                        out.flush();

                        if (out != null) {
                            out.close();
                        }
                    } catch (Exception e) {
                        try {
                            if (out != null) {
                                out.close();
                            }
                        } catch (IOException e1) {
                            System.out.print("write errors!" + e);
                        }

                        System.out.print("write errors!" + e);
                    }
                }
            問題解決。

            posted on 2013-02-04 15:38 小果子 閱讀(2939) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 學(xué)習(xí)筆記
            欧美一区二区精品久久| 伊人久久大香线蕉无码麻豆| 久久香综合精品久久伊人| 国产精品美女久久久久av爽| 精品久久久久久久久中文字幕| 国产人久久人人人人爽| 99久久综合狠狠综合久久止| 久久婷婷五月综合色高清| 久久亚洲国产成人精品性色| 久久亚洲欧美国产精品| 国产成人综合久久综合| 久久免费视频网站| 久久99国产一区二区三区| 久久久久97国产精华液好用吗| 2021国产成人精品久久| 精品久久久久一区二区三区| 亚洲AⅤ优女AV综合久久久| 久久综合亚洲色一区二区三区| 亚洲AV日韩精品久久久久| 久久久婷婷五月亚洲97号色| 久久青青草原综合伊人| 久久精品国产欧美日韩| 国产成人精品综合久久久久 | 久久久久久精品成人免费图片| 中文成人无码精品久久久不卡 | 少妇熟女久久综合网色欲| 无码久久精品国产亚洲Av影片 | 亚洲人成无码网站久久99热国产| 思思久久99热只有频精品66| 久久国产亚洲精品无码| 久久综合狠狠综合久久97色| 一本色道久久综合狠狠躁| 99久久婷婷国产一区二区| 久久亚洲国产精品成人AV秋霞| 97久久综合精品久久久综合| 久久青青草原亚洲av无码| 91久久婷婷国产综合精品青草 | 国产精品永久久久久久久久久| 久久久久亚洲AV片无码下载蜜桃| 国产精品国色综合久久| 成人综合久久精品色婷婷|