• <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)狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預(yù)料,人們需要更細(xì)心的觀察別人,要隨時注意才能保護(hù)別人,因為他們未必知道自己要什么·····

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

            1、中文字符是gbk(gb2312)

            有兩種解決方法

            第一種:

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

            strpos($curl_res, ‘哈哈’)

            第二種:

            將PHP保存為UTF-8無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無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ù)時要考慮到!


                 我生成的那個html文件被EmEditor認(rèn)為UTF-8 with Signature。而好用的那個html文件被EmEditor認(rèn)為UTF-8 without Signature.
                對于這兩種UTF-8格式的轉(zhuǎn)換,我查看了網(wǎng)上信息,點擊記事本,EmEditor等文本編輯器的另存為,當(dāng)選擇了UTF-8的編碼格式時,Add a Unicode Signature(BOM)這個選項被激活,只要選擇上,我的文件就可以存為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
            我大致了解了他們兩個的區(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以兩個字節(jié)為編碼單元,在解釋一個UTF-16文本前,首先要弄清楚每個編碼單元的字節(jié)序。 例如收到一個“奎”的Unicode編碼是594E,“乙”的Unicode編碼是4E59。如果我們收到UTF-16字節(jié)流“594E”,那么這是 “奎”還是“乙”?

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

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

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

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

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


            原來BOM是在文件的開始加了幾個字節(jié)作為標(biāo)記。有了這個標(biāo)記,一些協(xié)議和系統(tǒng)才能識別。好,看看怎么加上這寫字節(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' );就是這個。
            于是我的代碼變?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) 評論(0)  編輯 收藏 引用 所屬分類: 學(xué)習(xí)筆記
            无码伊人66久久大杳蕉网站谷歌 | 色欲av伊人久久大香线蕉影院| 91精品婷婷国产综合久久| 久久99中文字幕久久| 久久久久国色AV免费看图片| 久久天天躁狠狠躁夜夜不卡| 人人狠狠综合久久88成人| 久久久国产精品网站| 久久无码AV一区二区三区| 狠狠人妻久久久久久综合| 久久综合给合久久狠狠狠97色69| 欧美久久精品一级c片片| 久久大香萑太香蕉av| 国产精品视频久久| 无码国内精品久久人妻蜜桃| 国产A级毛片久久久精品毛片| 久久99国产综合精品女同| 精产国品久久一二三产区区别| 久久精品国产欧美日韩| 久久免费的精品国产V∧| 精品久久久久久无码专区 | 国产99久久久国产精品小说| 波多野结衣中文字幕久久| 久久99精品国产麻豆不卡| 国内精品久久久久影院一蜜桃| 久久久久人妻一区二区三区 | 精品久久久久久无码中文字幕 | 欧美久久一级内射wwwwww.| 精品久久久久久中文字幕人妻最新| 一本久道久久综合狠狠躁AV| 久久高潮一级毛片免费| 久久久综合九色合综国产| WWW婷婷AV久久久影片| 久久久精品国产sm调教网站| 日韩欧美亚洲综合久久| 亚洲欧美一级久久精品| 久久一本综合| 久久亚洲精品无码aⅴ大香| 亚洲人成网站999久久久综合 | a级成人毛片久久| 久久国产乱子精品免费女|