PHP implements full-width character conversion to half-width method summary, full-width half-width

Source: Internet
Author: User

PHP implements full-width character conversion to half-width method summary, full-width half-width

The simplest method

<?php  $str = "0123ABCDFWS\",.?<>{}[]*&^%#@!~()+-|:;";  echo "$str";  echo "<br />";  $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 ','-','',':','. ','/',' % ','#','! ',' @ ',' & ',' (',') ',' <','> ','"','\'','? ',' [','] ',' {','} ',' \ ',' | ',' + ',' = ','_', '^', '¥ ','~ ', '''); 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.

Traditionally, English or Latin letters are stored in one byte, while Chinese characters and Japanese are usually stored in two bytes, in order to make the font look neat, English letters, numbers, and other symbols also use only one word space and are displayed in two bytes.

The above is all the content of this article. I hope you will like it.

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.