如何校验是否输入全角字符!

解决方案 »

  1.   

    参考这个帖子中的做法:
    http://topic.csdn.net/t/20040316/11/2848001.html引用一下:function   ChgTitle($title)    
      {    
       
      $length   =   46;    
      if   (strlen($title)>$length)   {    
      $temp   =   0;    
      for($i=0;   $i<$length;   $i++)    
      if   (ord($title[$i])   >   128)    
      $temp++;    
      if   ($temp%2   ==   0)    
      $title   =   substr($title,0,$length)."...";    
      else    
      $title   =   substr($title,0,$length+1)."...";    
      }    
      return   $title;    
      }    
       
      原理就是截断一个字符,看看其ascII码是不是大于128,如果是,说明截断的是一个全角汉字,那么就退后一个截断。用$length控制长度    
       
      备注:循环判断字符串里面的   >128   的字符个数,如果半角字符为偶数,则表示位置刚好为整个汉字,如果为奇数,则为半个汉字,需要取下一个字符
      

  2.   

    public string WideCharToChar(string source)
            {
                System.Text.StringBuilder result = new System.Text.StringBuilder(source.Length, source.Length);
                for (int i = 0; i < source.Length; i++)
                {
                    if (source[i] >= 65281 && source[i] <= 65373)
                    {
                        result.Append((char) (source[i] - 65248));
                    }
                    else if (source[i] == 12288)
                    {
                        result.Append(' ');
                    }
                    else
                    {
                        result.Append(source[i]);
                    }
                }
                return result.ToString();
            }
    这也有个方法