最简单的方法
PHP;">
PHP
$str = "0123ABCDFWS\",.?<>{}[]*&^%#@!~()+-|:;";
echo "$str";
echo "
"; $str = preg_replace('/\xa3([\xa1-\xfe])/e','chr(ord(\1)-0x80)',$str); echo $str;
"; $str = preg_replace('/\xa3([\xa1-\xfe])/e','chr(ord(\1)-0x80)',$str); echo $str;
这是网上看来的代码,所有的中文标点的第二个字节减去0X80(即128)所得的数字就是半角所得的数字了。而/e模式表达的是:如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串。
在非UTF-8模式下这个函数是可行的,但是UTF-8下 这个方法就似乎无效,
方法二:
'0','1' => '1','2' => '2','3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9','A' => 'A','B' => 'B','C' => 'C','D' => 'D','E' => 'E','F' => 'F','G' => 'G','H' => 'H','I' => 'I','J' => 'J','K' => 'K','L' => 'L','M' => 'M','N' => 'N','O' => 'O','P' => 'P','Q' => 'Q','R' => 'R','S' => 'S','T' => 'T','U' => 'U','V' => 'V','W' => 'W','X' => 'X','Y' => 'Y','Z' => 'Z','a' => 'a','b' => 'b','c' => 'c','d' => 'd','e' => 'e','f' => 'f','g' => 'g','h' => 'h','i' => 'i','j' => 'j','k' => 'k','l' => 'l','m' => 'm','n' => 'n','o' => 'o','p' => 'p','q' => 'q','r' => 'r','s' => 's','t' => 't','u' => 'u','v' => 'v','w' => 'w','x' => 'x','y' => 'y','z' => 'z');
echo preg_replace_callback("/([\xEF][\xBC][\x90-\x99]|[\xEF][\xBD][\x81-\x9A\xA1-\xBA])/",'next_fchar','0');
function next_fchar($matches){
global $queue;
return $queue[$matches[1]];
}
方法三:
PHP;">
/**
* 字符串半角和全角间相互转换
* @param string $str 待转换的字符串
* @param int $type TODBC:转换为半角;TOSBC,转换为全角
* @return string 返回转换后的字符串
*/
function convertStrType($str,$type) {
$dbc = array(
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-',' ',':','.',',','/','%','#','!','@','&','(',')','<','>','"',''','?','[',']','{','}','\','|','+','=','_','^','¥',' ̄','`'
);
$sbc = array( //<a href="/tag/banjiao/" target="_blank" class="keywords">半角</a>
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-',' ',':','.',','/','%',' #','!','@','&','(',')','<','>','"','\'','?','[',']','{','}','\\','|','+','=','_','^','~','`'
);
if($type == 'TODBC'){
return str_replace( $sbc,$dbc,$str ); //半角到全角
}elseif($type == 'TOSBC'){
return str_replace( $dbc,$sbc,$str ); //全角到半角
}else{
return $str;
}
}
方法四:
'0','z' => 'z','(' => '(',')' => ')','〔' => '[','〕' => ']','【' => '[','】' => ']','〖' => '[','〗' => ']','“' => '[','”' => ']','‘' => '[',''' => ']','{' => '{','}' => '}','《' => '<','》' => '>','%' => '%','+' => '+','—' => '-','-' => '-','~' => '-',':' => ':','。' => '.','、' => ',',' => '.','、' => '.',';' => ','?' => '?','!' => '!','…' => '-','‖' => '|','”' => '"',''' => '`','‘' => '`','|' => '|','〃' => '"',' ' => ' ');
return strtr($str,$arr);
}
全角,又称全形、全宽,是电脑字符的一种格式,字面意思是比普通字符(或半角字符)宽的字符。
传统上,英语或拉丁字母语言使用一字节的空间来存储,而汉字、日语等常使用两字节存储,在使用固定宽度文字的地方,为了使字体看起来整齐,英文字母、数字及其他符号,也由原来只占用一个字空间,改为一概占用两个字的空间来显示,并且使用两个字节来存储。
以上所述就是本文的全部内容了,希望大家能够喜欢。