• <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 - 我并不覺(jué)的自豪,我所嘗試的事情都失敗了······習(xí)慣原本生活的人不容易改變,就算現(xiàn)狀很糟,他們也很難改變,在過(guò)程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛(ài)傳出去,很困難,也無(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的格式。可是,問(wèn)題就在于,我用java怎么讓我的文件直接生成為 UTF-8 with Signature的格式。
                開(kāi)始上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é)為編碼單元,沒(méi)有字節(jié)序的問(wèn)題。UTF-16以?xún)蓚€(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"又被稱(chēng)作BOM。

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

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


            原來(lái)BOM是在文件的開(kāi)始加了幾個(gè)字節(jié)作為標(biāo)記。有了這個(gè)標(biāo)記,一些協(xié)議和系統(tǒng)才能識(shí)別。好,看看怎么加上這寫(xiě)字節(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);
                    }
                }
            問(wèn)題解決。

            posted on 2013-02-04 15:38 小果子 閱讀(2939) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 學(xué)習(xí)筆記
            久久精品国产清自在天天线 | 久久国产AVJUST麻豆| 久久精品免费网站网| 欧美激情精品久久久久久久九九九 | 久久精品18| 久久99精品久久久久久hb无码 | 久久久久99这里有精品10| 久久久久亚洲AV片无码下载蜜桃| 久久中文娱乐网| 久久精品国产AV一区二区三区| 国产国产成人精品久久| 亚洲精品综合久久| 久久亚洲国产欧洲精品一| 久久只有这精品99| 国产激情久久久久影院老熟女| 久久综合久久性久99毛片| 嫩草伊人久久精品少妇AV| 亚洲国产精品综合久久网络| 久久国产色AV免费看| 伊人久久大香线蕉综合Av| 国产精品久久久久久久午夜片| 久久无码人妻一区二区三区午夜| 香蕉久久永久视频| 国产精品免费看久久久香蕉| 久久er热视频在这里精品| 久久精品蜜芽亚洲国产AV| 亚洲国产天堂久久综合| 精品久久久久久久久久久久久久久| 亚洲va久久久噜噜噜久久| 一本一本久久a久久综合精品蜜桃| 91精品久久久久久无码| 成人免费网站久久久| 人妻精品久久无码专区精东影业| 久久强奷乱码老熟女网站| 亚洲国产成人精品91久久久| 久久99久久成人免费播放| 国内精品久久久久久麻豆| 久久精品国产福利国产秒| 久久精品男人影院| 久久美女网站免费| 久久精品国产精品亜洲毛片|