用正则表达式吧,php有支持二进制标示的正则表达式,可以处理。

解决方案 »

  1.   

    判断ascii, < 0 为汉字//strChinese 一行中所有的中文
    void CFindChineseCharDlg::AddOneSection(char* pBuf, DWORD& dwPos, std::string& strChinese)
    {
        ASSERT(pBuf);
        pBuf[dwPos] = 0;
        
        if (strChinese.size() > 0)
        {
            strChinese += std::string("  "); //中间以两个空格连接起来
        }
        strChinese += pBuf;
        
        dwPos = 0;
    }void CFindChineseCharDlg::LoadFile(const std::string& strFileName)
    {
        std::ifstream streamIn(strFileName.c_str());
        std::string strLine;
        TiSGetLine<char> getline;
        static char buf[1024 * 1024]; // 最长的一行总不至于超过一兆吧
        DWORD dwLineNum = 0; //行号
        DWORD dwPos;
        size_t i;
        char c1;
        
        //std::string strSent;    //一个连续的中文
        std::string strChinese; //一行中所有的中文
        
        m_strOutFileName = "c:\\FindChineseCharResult.txt";
        CHxTextFile outputFile(m_strOutFileName);
        
        try
        {
        while (getline.do_get_line(streamIn, strLine))
        {
            dwLineNum++;
            
            dwPos = 0;
            //strSent = "";
            strChinese = "";
            for(i = 0; i < strLine.size(); ++i)
            {
                c1 = strLine[i];
                if (c1 < 0) // 遇到汉字了
                {
                    buf[dwPos++] = c1;
                }
                else
                {
                    if (dwPos > 0) // 前面出现过中文字符
                    {
                        AddOneSection(buf, dwPos, strChinese);
                    }
                }
            }
            
            if (dwPos > 0) // 前面出现过中文字符
            {
                AddOneSection(buf, dwPos, strChinese);
            }
            
            //buf[dwPos] = 0;
            //MessageBox(buf);
            if (strChinese.size() > 0)
            {
                char szLineNum[10];
                //itoa(dwLineNum, szLineNum, 10);
                sprintf(szLineNum, "%-3d", dwLineNum);
                outputFile.WriteOneLine(std::string(szLineNum) + std::string(" ") + strChinese);
            }
        }
        }
        catch(...)
        {
            CString strError;
            strError.Format("第%d行出错", dwLineNum);
            MessageBox(strError);
        }
        if (!outputFile.IsEmpty())
        {
            outputFile.Close();
            ShellExecute(this->m_hWnd,"open",m_strOutFileName,NULL, NULL, SW_SHOWNORMAL);
        }
    }
      

  2.   

    gb18030的判断方法和unicode是不一样的。