PHP full-width-to-half-width code

Source: Internet
Author: User
Returns a string containing full-width numbers, letters, spaces, or The code is as follows:
/**
* Convert a string containing full-width numeric characters, letters, spaces, or '% +-()' to the corresponding half-width characters.
* @ Access public
* @ Param string $ str string to be converted
* @ Return string $ str string after processing
*/
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 );
}


The code is as follows:
$ Str = "0123ABCDFWS \",.? <>{} [] * & ^ % #@!~ () +-| :;";
Echo "$ str ";
Echo"
";
$ Str = preg_replace ('/\ xa3 ([\ xa1-\ xfe])/E', 'chr (ord (\ 1)-0x80 )', $ str );
Echo $ str;
?>


We also share a function

In many forms that collect user information, users are expected to enter half-width characters. However, no matter how emphasized or reminded, careless users may submit full-width data. In fact, in the eyes of some new users, there is no difference between the full angle and the half angle. even they may think that the information I submitted is clearly correct. why is it wrong?

In fact, the fullwidth and halfwidth have a one-to-one correspondence relationship, and most users do not intentionally enter wrong information. Therefore, we can avoid this situation through programs, you can give the user a confirmation opportunity after the switch, which may give the user a better experience.

The following describes a function to implement mutual conversion between the Halfwidth and fullwidth.

The code is as follows:
Header ("Content-type: text/html; charset = utf-8 ");

// The first parameter: input the string to be converted
// The second parameter: 0, Halfwidth to fullwidth; 1, fullwidth to halfwidth
Function SBC_DBC ($ str, $ args2 ){
$ 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 ','-','',':',
'.', '/', '% ','#',
'! ','@','&','(',')',
'<', '> ','"',''','? ',
'[', ']', '{', '}', '\',
'|', '+', '=', '_', '^ ',
'¥', 'Yun', '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', '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 ($ args2 = 0 ){
Return str_replace ($ SBC, $ DBC, $ str); // halfwidth to fullwidth
} Else if ($ args2 = 1 ){
Return str_replace ($ DBC, $ SBC, $ str); // full-width to half-width
} Else {
Return false;
}
}

$ Str1 = "http://www.bitsCN.com /-";
$ Str2 = "http://JB51.net /-";

Echo "halfwidth to fullwidth:
";
Echo $ str1. '->'. SBC_DBC ($ str1, 0 );

Echo"

Fullwidth to halfwidth:
";
Echo $ str2. '->'. SBC_DBC ($ str2, 1 );
?>

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.