[Regular expression] how to write and judge whether the submitted user name does not contain the following special characters? Method

Source: Internet
Author: User
[Regular] how do I write and determine whether the submitted user name does not contain the following special characters? At last, the request for this post was edited by default7 from 2014-05-2518: 39: 45 for the submitted registration username & nbsp; $ uname & nbsp;: 1 & nbsp; the name must be a mix of Chinese Characters & nbsp; English characters, but must not contain spaces 2 & nbsp; it cannot contain the following [regular]. how do I write and determine whether the submitted user name does not contain the following special characters? Method

This post was last edited by default7 at 18:39:45

Requirement for submitted registration username $ uname:
1. the name must consist of English and Chinese characters, but the name cannot contain spaces.
2 cannot contain the following special characters in $ arr2
3. it can contain Chinese brackets (")".


Function checkReg ($ uname)
{
/**
// Write a short
$ Arr2 = array (
'~ ','! ',' @ ',' # ',' $ ',' % ',' ^ ',' & ',' * ',' _ ',' + ', '|', '-', '= ','\\',
'{', '}', '[', ']', ':', ';', '"', '\', '<', '> ',',','. ','? ','/',
''',''','【',']','~ ','! ',' ¥ ','...... ',' -- ','. ',
PHP_EOL, chr (10), chr (13), "\ t", chr (32 ),
);
**/
$ Arr2 = array (
'~ ',
'! ',
'@',
'#',
'$ ',
'% ',
'^ ',
'&',
'*',
'_',
'+ ',
'| ',
'-',
'= ',
'\\',
'{',
'}',
'[',
']',
':',
';',
'"',
'\'',
'<',
'> ',
',',
'.',
'? ',
'/',
',
'"',
''',
''',
'【',
']',
'~ ',
'! ',
'¥ ',
'...... ',
'--',
',',
'《',
'",
'. ',
PHP_EOL,
Chr (10 ),
Chr (13), // \ r \ n
"\ T ",
Chr (32 ),
);


Foreach ($ arr2 as $ k ){
// I used preg_match to make the results messy. Later, strpos (default7 # zbphp.com) was used)
If (strpos ($ uname, $ k )! = False ){
$ Tips = "The registration name cannot contain special characters such as spaces, dots, and commas! ";

Return array (false, $ tips );
}
}

Return array (true, 'OK ');
}



------ Solution --------------------
If ($ s = str_replace ($ arr2, '', $ s )){
// Does not contain special characters
} Else {
// Contains special characters
}

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.