Php regular code verification string solution

Source: Internet
Author: User
Regular php regular code verification string rules: Can be combined with pure numbers or letters, only "-" can be used, but cannot contain Chinese characters or other characters. the character string cannot start with "-". cannot end with '-' ------ solution -------------------- PHPcodepreg_match (^ [a-z0-9] + [a-z0-9-] * [a-z0-9] + $ I, $ string ); ---- evaluate the php regular code to verify the string
String rules: you can use pure numbers or letters to combine them. you can only use "-", but cannot use Chinese characters or other characters. the character string cannot start with "-". it cannot end with "-".

------ Solution --------------------
PHP code
Preg_match ('/^ [a-z0-9] + [a-z0-9-] * [a-z0-9] + $/I', $ string );
------ Solution --------------------
I just pick the wrong one,

The first floor cannot match a single character :)
The second floor is also, \ w also contains _, the landlord does not want
------ Solution --------------------
The regular expression on the first floor is slightly modified to accommodate one character:
PHP code
/^ [A-z0-9] + [a-z0-9-] * [a-z0-9] + | [a-z0-9] + $/I
------ Solution --------------------
$ Str = '\ w + [0-9a-zA-Z \-] + \ w $'
------ Solution --------------------
I am also coming to join in
PHP code
/^ [0-9a-z] [-0-9a-z] * [0-9a-z] $/I

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.