Home  >  Article  >  Backend Development  >  Summary of commonly used PHP regular expressions

Summary of commonly used PHP regular expressions

WBOY
WBOYOriginal
2016-08-08 09:30:39807browse

Collection of commonly used regular expressions in PHP:
Regular expressions for matching Chinese characters: [u4e00-u9fa5]
Comment: Matching Chinese is really a headache, but with this expression it will be easier
Matching double bytes Characters (including Chinese characters): [^x00-xff]
Comment: Can be used to calculate the length of a string (a double-byte character length counts as 2, ASCII characters count as 1)
Regular expression to match blank lines: ns *r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: <(S*?)[^>]*>.*?|<.*? / >
Comment: The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags.
Regular expression for matching leading and trailing whitespace characters: ^s*|s*$
Comment: It can be used To delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of the line, a very useful expression
Regular expression for matching email addresses: w+([-+.]w+)*@w+ ([-.]w+)*.w+([-.]w+)*
Comment: Very useful for form validation
Regular expression for matching URL: [a-zA-z]+://[^s] *
Comment: The version circulating on the Internet has very limited functions. The above one can basically meet the needs.
Is the matching account legal (starting with a letter, 5-16 bytes are allowed, alphanumeric underscores are allowed): ^[a-zA-Z][a -zA-Z0-9_]{4,15}$
                                                      use use using using form                   ( d{4)           -                                                                 -          The format is like 0511-4405222 or 021-87888822
Matching Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts from 10000
Matching Chinese postal code: [1-9]d {5}(?!d)
Comment: China’s postal code is 6 digits
Matching ID card: d{15}|d{18}
Comment: China’s ID card is 15 or 18 digits
Matching IP address: d+.d+.d+.d+
                                                      ‐                                                              . Integer
^-?[1-9]d*$ // Match integer
^[1-9]d*|0$ // Match non-negative integer (positive integer + 0)
^-[1-9]d *|0$ // Match non-positive integers (negative integers + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ // Match positive floating point numbers
^- ([1-9]d*.d*|0.d*[1-9]d*)$ // Match negative floating point numbers
^-?([1-9]d*.d*|0.d *[1-9]d*|0?.0+|0)$ //Match floating point numbers
^[1-9]d*.d*|0.d*[1-9]d*|0? .0+|0$ // Match non-negative floating point numbers (positive floating point numbers + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))| 0?.0+|0$ // Match non-positive floating point numbers (negative floating point numbers + 0)
    Comments: Useful when processing large amounts of data, please pay attention to corrections in specific applications
      Match specific strings:
    ^[A-Za-z ]+$  // Matches a string consisting of 26 English letters
^[A-Z]+$  // Matches a string consisting of 26 uppercase English letters
^[a-z]+$  // Matches 26 English letters A string consisting of lowercase letters
^[A-Za-z0-9]+$  // Matches a string consisting of numbers and 26 English letters
^w+$  // Matches a string consisting of numbers, 26 English letters or underscores A string composed of

The above has introduced a summary of commonly used PHP regular expressions, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn