PHP string processing full-width half-width conversion, PHP string full-width half-width _php tutorial

Source: Internet
Author: User

PHP string processing full-width half-width conversion, PHP string full-width half angle


Half-angle full-width processing is a common problem in string processing, this paper attempts to provide a way for everyone.

First, the concept

Full-width character Unicode encoding from 65281~65374 (hex 0xff01 ~ 0xff5e)
Half-width character Unicode encoding from 33~126 (hex 0x21~ 0x7E)
Space is special, full angle is 12288 (0x3000), half angle is (0x20)
And in addition to empty, the full-width/half-width of Unicode-encoded sorting is corresponding in order
So you can deal with non-whitespace data directly by using +---to handle the whitespace separately.

Second, the realization of ideas

1. Find the character of the target Unicode, you can use regular expressions to resolve

2. Modifying Unicode encoding

Third, the realization

1. The first is the two Unicode and character conversion functions:

1     /**2 * Convert Unicode to characters3 * @param int $unicode4 * @return string UTF-8 characters5      **/6     functionUnicode2char ($unicode){7         if($unicode< 128)return CHR($unicode);8         if($unicode< 2048)return CHR(($unicode>> 6) + 192).9                                       CHR(($unicode& 63) + 128);Ten         if($unicode< 65536)return CHR(($unicode>> 12) + 224). One                                       CHR((($unicode>> 6) & 63) + 128). A                                       CHR(($unicode& 63) + 128); -         if($unicode< 2097152)return CHR(($unicode>> 18) + 240). -                                       CHR((($unicode>>) & 63) + 128). the                                       CHR((($unicode>> 6) & 63) + 128). -                                       CHR(($unicode& 63) + 128); -         return false; -     } +   -     /** + * Convert characters to Unicode A * @param string $char must be a UTF-8 character at * @return int -      **/ -     functionChar2unicode ($char){ -         Switch(strlen($char)){ -              Case1:return Ord($char); -              Case2:return(Ord($char{1}) & 63) | in((Ord($char{0}) & << 6); -              Case5 |return(Ord($char{2}) & 63) | to((Ord($char{1}) & << 6) | +((Ord($char{0}) & << 12); -              Case4:return(Ord($char{3}) & 63) | the((Ord($char{2}) & << 6) | *((Ord($char{1}) & << 12) | $((Ord($char{0}) & 7) << 18);Panax Notoginseng             default: -                 Trigger_error(' Character is not utf-8! ',e_user_warning); the                 return false; +         } A}

2. Full-width turning half angle

1     /* * 2      * Full-width turn half angle 3     * @param string $str  4     * @return string  5       **/  6     function sbc2dbc ($str) { 7         return Preg_replace (  8             //  9             '/[\x{3000}\x{ff01}-\x{ff5f}]/ue ',+             //  code conversion  One             //0x3000 is a space, special processing, other full-width character encoding -0xfee0 can be converted to half-width              ($unicode =char2unicode (\ ' \0\ ')) = = 0x3000? "": (($code = $unicode -0xfee0) > 256? Unicode2char ($code): Chr ($code)) ',             $str        ];      }

3. Half angle turning full angle

  1  /*   *   2   * Half width turn full angle   3  * @param string $str   4   * @return string   5   *  */  6   function  dbc2sbc ( $str  ) {  7   return   preg_replace   (  8  //  Half-width character   9  '/[\x{0020}\x{0020 }-\x{7e}]/ue ',  ten  //  encoding conversion   one  //0x0020 is empty Lattice, special processing, other half-width character encoding +0xfee0 can be converted to full-width    ' ($unicode =char2unicode (\ ' \0\ ')) = = 0x0020?             Unicode2char (0x3000): (($code = $unicode +0xfee0) > Unicode2char ($code): Chr ($code)) ',    $str    );   } 

Iv. Testing

Example code:

1 $a = ' abc12 345 '; 2 $SBC = DBC2SBC ($a); 3 $dbc = Sbc2dbc ($sbc); 4 5 Var_dump ($a$sbc$dbc);

Results:

1 string (9) "ABC12 345"2string"Abc12 345"3string(9) "ABC12 345"

http://www.bkjia.com/PHPjc/1102854.html www.bkjia.com true http://www.bkjia.com/PHPjc/1102854.html techarticle PHP string processing of full-width half-width conversion, PHP string full-width half-width corner of the full-width of the processing of string processing is a common problem, this article attempts to provide you with a thought. First, the concept of the whole ...

  • 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.