首页 > 代码库 > 用perl对字符串进行全角转半角操作

用perl对字符串进行全角转半角操作

#全角转半角
#ljl
use Encode;

sub CheckSBC
{
	my ($name)=@_;
	my @character;
	my $tmpname=$name;
	my $headname;
	my $k=0;
	for($ii=1; $ii <= length($name); $ii++) 
	{
		if(length($tmpname)>0)
		{
			my $headname2=substr($tmpname,0,1);
		 	if (ord($headname2) >= 128)
			{
				$headname2=decode("gbk",substr($tmpname,0,2));
				if(ord($headname2)==12288)
				{
					$headname2=chr(32);	
					$headname=$headname.$headname2;
					$k=1;
				}
				elsif(ord($headname2)>65280 and ord($headname2)<65375)
				{
					$headname2=encode("gbk",chr(ord($headname2)-65248));  
					$headname=$headname.$headname2;
					$k=1;
				}
				else
				{ 
					$headname=$headname.encode("gbk",$headname2);
					$k=1;
				}
				$tmpname=substr($tmpname,2,length($tmpname)-2);
				$ii++;
			}
			else
			{
				$headname=$headname.$headname2;
				$tmpname=substr($tmpname,1,length($tmpname)-1);
			}
		}
	}
	$name=$headname;
  return $name;  		
}