Solving the problem of PHP regular code validation string

Source: Internet
Author: User
Find the PHP regular code validation string for the segment
String rules: Can be combined with pure or pure letters, can only use "-" can not use Chinese characters, other character symbols and the beginning of the string cannot be "-", the end can not be "-"

------Solution--------------------
PHP Code
Preg_match ('/^[a-z0-9]+[a-z0-9-]*[a-z0-9]+$/i ', $string);
------Solution--------------------
I'm just picking the wrong

1 floor match not one character:)
2 floor is also, \w also contains _, landlord do not
------Solution--------------------
The 1 floor of the regular is slightly modified to fit the case of a 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'm here to make a fuss.
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.