/**
* Convert Unicode to Characters
* @param int $unicode
* @return String UTF-8 characters
**/
function Unicode2char ($unicode) {
if ($unicode <) return Chr ($unicode);
if ($unicode < 2048) return Chr (($unicode >> 6) + 192).
Chr (($unicode & 63) + 128);
if ($unicode < 65536) return Chr (($unicode >> 12) + 224).
Chr ((($unicode >> 6) & 63) + 128).
Chr (($unicode & 63) + 128);
if ($unicode < 2097152) return Chr (($unicode >> 18) + 240).
Chr ((($unicode >>) & 63) + 128).
Chr ((($unicode >> 6) & 63) + 128).
Chr (($unicode & 63) + 128);
return false;
}
/**
* Convert characters to Unicode
* @param string $char must be a UTF-8 character
* @return int
**/
function Char2unicode ($char) {
Switch (strlen ($char)) {
Case 1:return Ord ($char);
Case 2:return (Ord ($char {1}) & 63) |
(Ord ($char {0}) &) << 6);
Case 3:return (Ord ($char {2}) & 63) |
((Ord ($char {1}) &) << 6) |
(Ord ($char {0}) & << 12);
Case 4:return (Ord ($char {3}) & 63) |
((Ord ($char {2}) &) << 6) |
((Ord ($char {1}) &) << 12) |
(Ord ($char {0}) & 7) << 18);
Default:
Trigger_error (' Character is not utf-8! ', e_user_warning);
return false;
}
}
/**
* Full angle turning half angle
* @param string $str
* @return String
**/
function Sbc2dbc ($STR) {
Return Preg_replace_callback (
Full-width characters
'/[\x{3000}\x{ff01}-\x{ff5f}]/u ',
function ($matches) {
Encoding Conversion
0x3000 is a space, special handling, other full-width character encoding -0xfee0 can be converted to half-width
return ($unicode = $this-Char2unicode ($matches [0]) = = = 0x3000? "": (($code = $unicode -0xfee0) > 256? $this-Unicode2char ($code): Chr ($code));
},
$STR);
}
/**
* Half angle turn full angle
* @param string $str
* @return String
**/
function DBC2SBC ($STR) {
Return Preg_replace_callback (
Half-width characters
'/[\x{0020}\x{0020}-\x{7e}]/u ',
function ($matches) {
Encoding Conversion
0X0020 is a space, special processing, other half-width character encoding +0xfee0 can be converted to full-width
return ($unicode = $this-Char2unicode ($matches [0]) = = = 0x0020? Unicode2char $this (0x3000): (($code = $unicode +0xfee0) > $this-Unicode2char ($code): Chr ($code));
},
$STR);
}
How to use
$STR = ' abc, Hello! ‘;
$str _q = $this, Sbc2dbc ($STR); Full angle turning half angle
$str _b = $this->DBC2SBC ($str _q); Half angle turn full angle
[PHP] Full-width character half-width characters convert to each other