[PHP] Full-width character half-width characters convert to each other

Source: Internet
Author: User
Tags ord

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.