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

Source: Internet
Author: User
PHP implements full-width character conversion to a half-width method to summarize the full-width half-width characters. PHP implements full-width character conversion to half-width method summary. The simplest method of full-width half-width is php $ str0123ABCDFWS \",.? <>{} [] * & ^ % #@!~ PHP implements full-width character conversion to half-width method summary, full-width half-width

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

The simplest method of callback is php $ str = "0123ABCDFWS \",.? <> {} [] * & ^ % #@!~...

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.