PHP常用正则表达式汇总
2. "^\d+$" //非负整数(正整数 + 0)
3. "^[0-9]*[1-9][0-9]*$" //正整数
4. "^((-\d+)|(0+))$" //非正整数(负整数 + 0)
5. "^-[0-9]*[1-9][0-9]*$" //负整数
6. "^-?\d+$" //整数
7. "^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0)
8. "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
9. "^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮点数(负浮点数 + 0)
10. "^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
11. "^(-?\d+)(\.\d+)?$" //浮点数
12. "^[A-Za-z]+$" //由26个英文字母组成的字符串
13. "^[A-Z]+$" //由26个英文字母的大写组成的字符串
14. "^[a-z]+$" //由26个英文字母的小写组成的字符串
15. "^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串
16. "^\w+$" //由数字、26个英文字母或者下划线组成的字符串
17. "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址
18. "^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
19. /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日
20. /^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年
21. "^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil
22. /^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/ //电话号码
23. "^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP地址
24.
25. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
26. 匹配双字节字符(包括汉字在内):[^\x00-\xff]
27. 匹配空行的正则表达式:\n[\s| ]*\r
28. 匹配HTML标记的正则表达式:/.*|/
29. 匹配首尾空格的正则表达式:(^\s*)|(\s*$)
30. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
31. 匹配网址URL的正则表达式:^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$
32. 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
33. 匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})?
34. 匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$
35.
36.
37. 元字符及其在正则表达式上下文中的行为:
38.
39. \ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。
40.
41. ^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。
42.
43. $ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。
44.
45. * 匹配前面的子表达式零次或多次。
46.
47. + 匹配前面的子表达式一次或多次。+ 等价于 {1,}。
48.
49. ? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。
50.
51. {n} n 是一个非负整数,匹配确定的n 次。
52.
53. {n,} n 是一个非负整数,至少匹配n 次。
54.
55. {n,m} m 和 n 均为非负整数,其中n
56.
57. ? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
58.
59. . 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任何字符,请使用象 ’[.\n]’ 的模式。
60. (pattern) 匹配pattern 并获取这一匹配。
61.
62. (?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。
63.
64. (?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。
65.
66. (?!pattern) 负向预查,与(?=pattern)作用相反
67.
68. x|y 匹配 x 或 y。
69.
70. [xyz] 字符集合。
71.
72. [^xyz] 负值字符集合。
73.
74. [a-z] 字符范围,匹配指定范围内的任意字符。
75.
76. [^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。
77.
78. \b 匹配一个单词边界,也就是指单词和空格间的位置。
79.
80. \B 匹配非单词边界。
81.
82. \cx 匹配由x指明的控制字符。
83.
84. \d 匹配一个数字字符。等价于 [0-9]。
85.
86. \D 匹配一个非数字字符。等价于 [^0-9]。
87.
88. \f 匹配一个换页符。等价于 \x0c 和 \cL。
89.
90. \n 匹配一个换行符。等价于 \x0a 和 \cJ。
91.
92. \r 匹配一个回车符。等价于 \x0d 和 \cM。
93.
94. \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
95.
96. \S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
97.
98. \t 匹配一个制表符。等价于 \x09 和 \cI。
99.
100. \v 匹配一个垂直制表符。等价于 \x0b 和 \cK。
101.
102. \w 匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’。
103.
104. \W 匹配任何非单词字符。等价于 ’[^A-Za-z0-9_]’。
105.
106. \xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。
107.
108. \num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。
109.
110. \n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。
111.
112. \nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。
113.
114. \nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。
115.
116. \un 匹配 n,其中 n 是一个用四个十六进制数字表示的Unicode字符。
117.
118. 匹配中文字符的正则表达式: [u4e00-u9fa5]
119.
120. 匹配双字节字符(包括汉字在内):[^x00-xff]
121.
122. 匹配空行的正则表达式:n[s| ]*r
123.
124. 匹配HTML标记的正则表达式:/.*1>|/
125.
126. 匹配首尾空格的正则表达式:(^s*)|(s*$)
127.
128. 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
129.
130. 匹配网址URL的正则表达式:http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
131.
132. 利用正则表达式限制网页表单里的文本框输入内容:
133.
134. 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
135.
136. 用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"
137.
138. 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
139.
140. 用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
141.
142. =========常用正则式
143.
144.
145.
146. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
147.
148. 匹配双字节字符(包括汉字在内):[^\x00-\xff]
149.
150. 匹配空行的正则表达式:\n[\s| ]*\r
151.
152. 匹配HTML标记的正则表达式:/.*|/
153.
154. 匹配首尾空格的正则表达式:(^\s*)|(\s*$)
155.
156. 匹配IP地址的正则表达式:/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //
157.
158. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
159.
160. 匹配网址URL的正则表达式:http://(/[\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
161.
162. sql语句:^(select|drop|delete|create|update|insert).*$
163.
164. 1、非负整数:^\d+$
165.
166. 2、正整数:^[0-9]*[1-9][0-9]*$
167.
168. 3、非正整数:^((-\d+)|(0+))$
169.
170. 4、负整数:^-[0-9]*[1-9][0-9]*$
171.
172. 5、整数:^-?\d+$
173.
174. 6、非负浮点数:^\d+(\.\d+)?$
175.
176. 7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
177.
178. 8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$
179.
180. 9、负浮点数:^(-((正浮点数正则式)))$
181.
182. 10、英文字符串:^[A-Za-z]+$
183.
184. 11、英文大写串:^[A-Z]+$
185.
186. 12、英文小写串:^[a-z]+$
187.
188. 13、英文字符数字串:^[A-Za-z0-9]+$
189.
190. 14、英数字加下划线串:^\w+$
191.
192. 15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
193.
194. 16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$
195. 或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^\"\"])*$
196.
197. 17、邮政编码:^[1-9]\d{5}$
198.
199. 18、中文:^[\u0391-\uFFE5]+$
200.
201. 19、电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$
202.
203. 20、手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$
204.
205. 21、双字节字符(包括汉字在内):^\x00-\xff
206.
207. 22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数)
208.
209. 23、匹配HTML标记:.*|
210.
211. 24、匹配空行:\n[\s| ]*\r
212.
213. 25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
214.
215. 26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
216.
217. 27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
218.
219. 28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
220.
221. 29、提取信息中的中国手机号码:(86)*0*13\d{9}
222.
223. 30、提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
224.
225. 31、提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
226.
227. 32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
228.
229. 33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+
230.
231. 34、提取信息中的任何数字 :(-?\d*)(\.\d+)?
232.
233. 35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+)
234.
235. 36、电话区号:/^0\d{2,3}$/
236.
237. 37、腾讯QQ号:^[1-9]*[1-9][0-9]*$
238.
239. 38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
240.
241. 39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
