This article mainly summarizes the methods for converting full-width characters into half-width characters in PHP. From Simplicity to complexity, you can choose freely based on your needs.
This article mainly summarizes the methods for converting full-width characters into half-width characters in PHP. From Simplicity to complexity, you can choose freely based on your needs.
The simplest method
<? Php $ str = "0123ABCDFWS \",.? <>{} [] * & ^ % #@!~ () +-|:; "; Echo" $ str "; echo"
"; $ Str = preg_replace ('/\ xa3 ([\ xa1-\ xfe])/E', 'chr (ord (\ 1)-0x80 )', $ str); echo $ str;
This is the code on the Internet. The second byte of all Chinese Punctuation Marks minus 0X80 (that is, 128) is the half-width number. The/e mode indicates that if this modifier is set, preg_replace () replaces the reverse reference in the replacement string and uses it as the PHP code value, replace the searched string with the result.
This function is feasible in non-UTF-8 mode, but this method does not seem to work in UTF-8,
Method 2:
$ Queue = Array ('0' => '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];}
Method 3:
/*** Convert the half-width string and the full-width string * @ param string $ str the string to be converted * @ param int $ type TODBC: convert to the half-width; TOSBC, convert to full angle * @ return string returns the converted 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 ','-','',':','. ','/',' % ','#','! ',' @ ',' & ',' (',') ',' <','> ','"',''','? ',' [','] ',' {','} ',' \ ',' | ',' + ',' = ','_', '^', '¥', ' ̄', 'Hangzhou'); $ sbc = array (// halfwidth '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', '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 ','-', '',':','. ',',',' http://www.jb51.net/ ',' % ','#','! ',' @ ',' & ',' (',') ',' <','> ','"','\'','? ',' [','] ',' {','} ',' \ ',' | ',' + ',' = ','_', '^', '¥ ','~ ', '''); If ($ type = 'todbin') {return str_replace ($ sbc, $ dbc, $ str ); // halfwidth to fullwidth} elseif ($ type = 'tosbc ') {return str_replace ($ dbc, $ sbc, $ str ); // fullwidth to halfwidth} else {return $ str ;}}
Method 4:
/*** Enclose a string with full-width numbers, letters, spaces, or '% + -() 'character conversion to the corresponding halfwidth character ** @ access public * @ param string $ str string to be converted ** @ return string $ str processed string */function make_semiangle ($ str) {$ arr = array ('0' => '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 ', '(' => '(', ')' => ')', '[' => '[', ']' => ']', '[' => '[', ']' => ']', '<' => '[', '<' => ']', '=>' [',' "'=>'] ', ''' =>' [', ''' =>'] ', '{' => '{', '}' => '}', '=>' <',' "'=> ', '%' => '%', '+' => '+', '-' => '-', '-' => '-','~ '=>'-',': '=> ':','. '=> '. ',', '=>', '=> '. ',', '=> '. ','; '=> ',','? '=> '? ','! '=> '! ','... '=>'-', ''' =>' | ',' "'=>'" ', ''' => ''', ''' => ''', '|' => '|', 'Region' => '"','' => ''); return strtr ($ str, $ arr );}
Difference between fullwidth and halfwidth (from Wikipedia)
Fullwidth, also known as fullwidth and fullwidth, is a format of computer characters. It literally means a character that is wider than a common character (or halfwidth character.