PHP for 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 half-width method Summary, full-width half-width


The simplest way

<?php  $str = "0123abcdfws\",.? <> {}[]*&^%#@! ~ () +-|:; ";  echo "$str";  echo "
"; $str = Preg_replace ('/\xa3 ([\xa1-\xfe])/E ', ' Chr (Ord (\1) -0x80) ', $str); Echo $str;

This is the online code, all the Chinese punctuation of the second byte minus 0x80 (that is, 128) the number is the half-width of the figure. The/E mode expresses: If this modifier is set, Preg_replace () makes a normal substitution of the inverse reference in the replacement string, evaluates it as a PHP code, and replaces the searched string with its result.

This function is feasible in non-UTF-8 mode, but this method seems to be invalid under UTF-8.

Method Two:

$queue = Array (' 0 ' = ' 0 ', ' 1 ' = ' 1 ', ' 2 ' = ' 2 ', ' 3 ' = ' 3 ', ' 4 ' = ' 4 ', ' 5 ' = ' + ' 5 ', ' 6 ' = ' 6 ' and ' 7 ' ' = ' + ' 7 ', ' 8 ' = ' 8 ', ' 9 ' = ' 9 ', ' A ' and ' a ', ' B ' = ' B ', ' c ' = ' C ', ' d ', ' = ' d ', ' e ' = ' e ', ' F ' = ' F ', ' g ' = ' g ', ' h ' = = ' h ', ' i ' = = ' I ', ' j ' = ' j ', ' k ' = ' k ', ' l ' = ', ' l ', ' m ' = ' + ', ' n ' =& Gt ' N ', ' o ' = ' o ', ' p ' = = ' P ', ' q ' + = ' q ', ' r ' = = ' R ', ' s ' + ' s ', ' t ' = ' t ', ' u ' = ' = ', ' V '  ', ' w ' = ' W ', ' x ' = ' x ', ' y ' = ' y ', ' z ' = ', ' z ', ' a ' = = ' A ', ' b ' = = ' B ', ' c ' = ' C ', ' d ' and '-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 Three:

/*** string Half-width and full-width conversion * @param string $str strings to be converted * @param int $type TODBC: Convert to Half-width, TOSBC, convert to full-width * @return string returns the converted string */funct        Ion 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 ', '-', ', ': ', '. ' , ',' , '/' , '%' , '#' , '! ', ' @ ', ' & ', ' (', ') ', ' ' ', ' ', ' ', ', ', '?    ', ' [', '] ', ' {', '} ', ' \ ', ' | ', ' + ', ' = ', ' _ ', ' ^ ', ' ¥ ', '  ̄ ', '); $SBC = Array (//half-width ' 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 ', '-', ', ': ', ', ', ', ', ', ', ', ', '/', '% ', ' # ', '! ', ' @ ', ' & ', ' (', ') ', ' < ', ' > ', ' ', ' ', '? ', ' [', '] ', ' {', '} ', ' \ \ ', ' | ', ' + ', ' = ', ' _ ', ' ^ ', ' ¥ ', ' ~ ', '), if ($type = = ' Todbc ') {return str_replace ($SB C, $dbc, $STR); Half-width to full-width}elseif ($type = = ' TOSBC ') {return str_replace ($DBC, $SBC, $STR),//full-width to half-width}else{return $str;}}

Method Four:

/*** converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to the corresponding half-width character * * @access public* @param string $str the string to be converted * * @return string $str processed string */fun Ction 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 ' = ' = ', ' m ' = ' + ' M ', ' n ' = ' n ', ' o ' = = ' o ', ' p ' = ' P ', ' q ' = ' Q ', ' R ' + ' R ', ' s ' and ' s ', ' t ' and ' t ', ' U ' =& Gt ' U ', ' V ' = ' V ', ' w ' = = ' W ', ' x ' = ' x ', ' y ' = ' y ', ' 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 ' =& Gt ' t ', ' u ' = =' U ', ' V ' = ' V ', ' w ' = = ' W ', ' x ' = ' x ', ' y ' = ' y ', ' z ' = ' = ', ' (' = ' = ' (') ', ' ' = ') ', ' (' = ' = ' [',  ' = ' + '] ', ' ' ' ' + ' [', ' ' ' ' + '] ', ' ' ' + ' [', ' ' ' + '] ', ' ' ' ' + ' [', ' ' ' ' = '] ', ' ' ' = = '] ', ' {' = = ' {', '} ' + '} ', ' ' = ' < ', ' ' = ' > ', '% ' = '% ', ' + ' + ' + ', '-' = '-', ' -' + '-', ' ~ ' = '-', ': ' = ': ', '. ' = = ', ', ', ' = = ', ', ', ' = ', ', ', ' = ', ', '; ' = ', ', '? ' = = '? ', '! ' = = '! ' = '-'-', ' ‖ ' and ' | ', ' ' ' ' ' and ' ' ' ', ' ' + ' ', ' ' ' + ' ', ' | ' + ' | ' Gt "); return Strtr ($str, $arr);}

The difference between full angle and half angle (from Chinese Wikipedia)

Full-width, also known as full-shape, full-width, is a form of computer characters that literally mean characters that are wider than ordinary characters (or half-width characters).

Traditionally, the English or Latin alphabet language uses a byte of space to store, and kanji, Japanese, etc. often use two bytes of storage, in the use of fixed-width text place, in order to make the font look neat, English letters, numbers and other symbols, but also by the original only occupy a word space, instead of occupying two words of space to display, and use two bytes to store it.

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1029595.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029595.html techarticle PHP to achieve full-width character conversion to half-width method Summary, the simplest method of full-width half-width 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.