Home >Backend Development >PHP Tutorial >Commonly used regular expressions in php (date, phone, Chinese, email, etc.)

Commonly used regular expressions in php (date, phone, Chinese, email, etc.)

WBOY
WBOYOriginal
2016-07-25 08:59:191145browse
Introducing some regular expressions commonly used in PHP, which can be used to match dates, phone numbers, Chinese and email addresses, etc. Friends in need can refer to it.

1, regular expression matching Chinese characters: [u4e00-u9fa5] Note: Matching Chinese is really a headache. With this expression, it will be easier. 2. Match double-byte characters (including Chinese characters): [^x00-xff] Description: Can be used to calculate the length of a string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1) 3. Regular expression to match blank lines: ns*r Description: Can be used to delete blank lines 4. Regular expression matching HTML tags: ]*>.*? 1> | <.> Note: 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. 5. Regular expression matching leading and trailing whitespace characters: ^s* |s*$ Description: It can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of the line. It is a very useful expression. 6. Regular expression matching email addresses: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* Note: Very useful for form validation 7. Regular expression matching URL: [a-zA-z]+://[^s]* Note: The version circulating on the Internet has very limited functions. The above one can basically meet the needs. 8. Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ Note: Very useful for form validation 9. Match domestic phone numbers: d{3}-d{8} |d{4}-d{7} Description: Matching format such as 0511-4405222 or 021-87888822 10. Match Tencent QQ number: [1-9][0-9]{4,} Note: Tencent QQ account starts from 10000 11. Match Chinese postal code: [1-9]d{5}(?!d) Note: China’s postal code is a 6-digit number 12. Matching ID card: d{15} |d{18} Note: China’s ID card has 15 or 18 digits 13. Match ip address: d+.d+.d+.d+ Note: Useful when extracting IP address 14. Match specific numbers:

^[1-9]d*$  ​​//Match positive integers ^-[1-9]d*$ //Match negative integers ^-?[1-9]d*$  ​​//Match integers ^[1-9]d* |0$  //Match non-negative integers (positive integers + 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 )

Note: Useful when processing large amounts of data, please pay attention to corrections when applying it. 15. Match specific string:

^[A-Za-z]+$  //Match a string consisting of 26 English letters ^[A-Z]+$ // Matches a string consisting of 26 uppercase English letters ^[a-z]+$ //Match a string consisting of 26 lowercase English 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

16, PHP regular date matching

$subject = '2011-01-01 38:20:26|1012657|4| |1'; $hour = rand(10,23); $hour = (strlen($hour)>1) ? $hour : "0{$hour}"; print_r(preg_replace('/(d+)-(d+)-(d+) (d+)/', '$bcw3388-${2}-${3} '.$hour, $subject));

That’s all. It’s a very practical PHP regular matching format. Open it and take a look when needed. It might be of great help.



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