How PHP converts full-width symbols in text to half-width

Source: Internet
Author: User
How does PHP convert the full-width symbol in text to half-width? This article will share a good package of conversion methods, you can refer to. We hope to help you.

Converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to corresponding half-width characters

/** * Converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to corresponding half-width characters * @access public * @param string $str to be converted String * @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 ' =&G T ' T ', u ' ~ = ' 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 ' =&G T ' s ', ' t ' = ' t ', ' u ' = ' u ', ' V ' = ' V ', ' w ' = ' W ', ' x ' = ' x ', ' y ' = ' y ', ' z ' = ' = ', ' (' = ' = ' (') ' ' = ') ', ' (' = ' + ' [', ') ' + '] ', ' ' ' ' + ' [', ' ' ' ' + '] ', ' ' ' = ' ' [', ' ' ' ' + '] ', ' ' ' ' + ' [', ' ' ' ' + '] ', ' =  > ' [', ' ' + '] ', ' {' = ' = ', '} ' = ', ' ' ' = ' < ', ' ' = ' > ', '% ' = '% ', ' + ' + ' + ', '-' = '-', '-'-' + '-', ' ~ ' + '-', ': ' + ': ', '. ' = = ', ', ', ' = = ', ', ', ' = ', ', ', ' = ', ', '; ' = ', ', '? ' = = '? ', '! ' = = '! ' = '-'-', ' ‖ ' and ' | ', ' ' ' ' ' ' ' ' ' ' ', ' ' ' + ' ', ' ' = ', ' ' ' ' ' ' '     > "); Return Strtr ($str, $arr); }
<?php     $str = "0123abcdfws\",.? <> {}[]*&^%#@! ~ () +-|:; ";     echo "$str";     echo "<br/>";     $str = Preg_replace ('/\xa3 ([\xa1-\xfe])/E ', ' Chr (Ord (\1) -0x80) ', $str);     echo $str;?>

Also share a function for everyone
In many forms of collecting user information, expect the user to input half-width characters, but no matter how you emphasize, how to remind, or there will be careless users to submit full-width data, in fact, in some novice eyes, full-angle and half-angle is no difference, even they will feel: I submitted clearly is the correct information, Why do you prompt for errors?

In fact, full-width and half-width have one by one correspondence, and most users do not intentionally enter the wrong information, so we can use the program to avoid this situation, the most, can be converted to give the user a chance to confirm, so that may be able to give users a better experience.

Here is a function that implements the reciprocal of the character half-width and the full-width

<?phpheader ("content-type:text/html;  Charset=utf-8 ");//First parameter: Pass in the string to be converted//second parameter: Take 0, half angle to full angle, take 1, full angle to half-width function sbc_dbc ($str, $args 2) {$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 ($args 2 = = 0) {return str_replace ($SBC, $DBC, $STR);  Half-width to full-width} else if ($args 2 = = 1) {return str_replace ($DBC, $SBC, $STR);    Full-width to half-width} else {return false; }} $str 1 = "http://", $STR 2 = "/http", echo "half-width to full-angle", "<br/>"; Echo $str 1. ' and '. SBC_DBC ($str 1, 0), echo "<br/><br/> Full-width turn half-angle: <br/>"; Echo $str 2. ' and '. Sbc_dbc ($str 2, 1);? >

Related recommendations:

PHP generates random strings that are readable

How PHP replaces multidimensional arrays with Str_replace

PHP for simple symmetric encryption

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.