PHP full angle to implement code _php tips

Source: Internet
Author: User
Copy Code code as follows:

/**
* Converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to the corresponding half-width characters
* @access Public
* @param string $str to convert string
* @return string $STR processing after strings
*/
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 ' =&gt ; ' 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 ' =& Gt ' 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 ', ' (' => ' (', ') ' => ') ', ' (' => ' [', '] ') ' => '] ', ' =&GT ' [', ' ' ' => '] ', ' ' => ' [', ' ' => '] ', ' ' ' => ', ' ' => ', ', ' ' => ' [', ' ' => '] ', ' {' => ', ', ' '} ' => '} ', ' "' => ' < ', '" ' => ' > ', '% ' => ', ' + ' => ' + ', '-' => '-', '-' => '-', ' ' => '- ', ': ' => ': ', '. ' => ', ', ', ' => ', ', ', ' => '. ', ', ' => '. ', ', ' => ', ', '? ' => '? ', '! ' => '! ', ' ... ' => '-', ' ‖ ' => ' | ', ' ' => ', ' ', ' ' => ', ' ' => ', ' ' | ' => ' | ', ' ' => ' ', ' ', ' > ");
Return Strtr ($str, $arr);
}


Copy Code code as follows:

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


Also share a function for everyone

In many forms that collect user information, expect the user to enter a half-width character, however, 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, the whole horn and half angle there is no difference, even they will feel: I submitted clearly is the correct information, Why would you be prompted with an error?

In fact, the full-width and Half-width have one by one correspondence, and most users do not intentionally input the wrong information, so we can use the program to avoid this situation, at 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 interchange between the character half-width and the full angle.

Copy Code code as follows:

<?php
Header ("content-type:text/html; Charset=utf-8 ");

First argument: Passing in the string to be converted
Second parameter: Take 0, half angle turn whole angle, take 1, full angle to half angle
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 angle
' 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 angle to full angle
else if ($args 2 = 1) {
Return Str_replace ($DBC, $SBC, $STR); Full angle to half angle
} else {
return false;
}
}

$str 1 = "http://www.jb51.net/-cloud-dwelling community";
$str 2 = "http://JB51." net/-Cloud-dwelling community ";

echo "Half angle turn whole angle: <br/>";
echo $str 1. '-> '. SBC_DBC ($str 1, 0);

echo "<br/><br/> Full angle: <br/>";
Echo $str 2. '-> '. Sbc_dbc ($str 2, 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.