10个实用的PHP正则表达式
正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符、词或算式等
正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符、词或算式等。但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时。本文为你介绍10种常见的实用PHP正则表达式的写法,希望对你的工作有所帮助。
1. 验证Email地址
这是一个用于验证电子邮件的正则表达式。但它并不是高效、完美的解决方案。在此不推荐使用。
1 2 3 4 5 6 7 8 | $email = "test@ansoncheung.tk" ; if (preg_match( '/^[^09][azAZ09_]+([.][azAZ09_]+)[@][azAZ09_]+([.][azAZ09_]+)[.][azAZ]{2,4}$/' ,$email)) { echo "Your email is ok." ; } else { echo "Wrong email address format" ; } |
为了更加有效验证电子邮件地址,推荐使用filer_var。
1 2 3 4 5 | if (filter_var( 'test+email@ansoncheung' , FILTER_VALIDATE_EMAIL)) { echo "Your email is ok." ; } else { echo "Wrong email address format." ; } |
2. 验证用户名
这是一个用于验证用户名的实例,其中包括字母、数字(AZ,az,09)、下划线以及最低5个字符,最大20个字符。同时,也可以根据需要,对最小值和最大值做合理的修改。
1 2 3 4 5 6 | $username = "user_name12" ; if (preg_match( '/^[azd_]{5,20}$/i' , $username)) { echo "Your username is ok." ; } else { echo "Wrong username format." ; } |
3. 验证电话号码
这是一个验证美国电话号码的实例。
1 2 3 4 5 6 | $phone = "(021)4232323" ; if (preg_match( '/(?d{3})?[s.]?d{3}[s.]d{4}/x' , $phone)) { echo "Your phone number is ok." ; } else { echo "Wrong phone number." ; } |
4. 验证IP地址
这是一个用来验证IPv4地址的实例。
1 2 3 4 5 6 7 8 | $IP = "198.168.1.78" ; if (preg_match( '/^(([19]?[09]|1[09]{2}|2[04][09]|25[05]).){3}([19]?[09]|1[09]{2}|2[04][09]|25[05])$/' ,$IP)) { echo "Your IP address is ok." ; } else { echo "Wrong IP address." ; } |
5. 验证邮政编码
这是一个用来验证邮政编码的实例。
1 2 3 4 5 6 | $zipcode = "123455434" ; if (preg_match( "/^([09]{5})([09]{4})?$/i" ,$zipcode)) { echo "Your Zip code is ok." ; } else { echo "Wrong Zip code." ; } |
6. 验证SSN(社会保险号)
这是一个验证美国SSN的实例。
1 2 3 4 5 6 | $ssn = "333232329" ; if (preg_match( '/^[d]{3}[d]{2}[d]{4}$/' ,$ssn)) { echo "Your SSN is ok." ; } else { echo "Wrong SSN." ; } |
7. 验证信用卡号
1 2 3 4 5 6 7 8 | $cc = "378282246310005" ; if (preg_match( '/^(?:4[09]{12}(?:[09]{3})?|5[15][09]{14}|6011[09]{12}|3(?:0[05]|[68][09])[09]{11}|3[47][09]{13})$/' , $cc)) { echo "Your credit card number is ok." ; } else { echo "Wrong credit card number." ; } |
8. 验证域名
1 2 3 4 5 6 7 8 | if (preg_match( '/^(http|https|ftp)://([AZ09][AZ09_](?:.[AZ09][AZ09_])+):?(d+)?/?/i' , $url)) { echo "Your url is ok." ; } else { echo "Wrong url." ; } |
9. 从特定URL中提取域名
1 2 3 4 | $host = $matches[ 1 ]; echo $host; |
10. 将文中关键词高亮显示
1 2 3 4 5 6 7 | $text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor"; $text = preg_replace( "/(regex)/i" , '1' , $text); echo $text; |
本文由 帝一博客 原创发布。用户在本站发布的原创内容(包括但不仅限于回答、文章和评论),著作权均归用户本人所有。独家文章转载,请联系邮箱:17762131@qq.com。获得授权后,须注明本文地址: https://d1blog.com/phpjiqiao/109.html
-
PHP 扫描微信公众号二维码,关注并自动登录网站
2020-10-27 16:35
-
fastadmin git 安装过程记录笔记
2020-10-03 17:34
-
PHP 多个redis key 删除遇到Function R...
2020-08-19 16:01
-
PHP-Redis,keys()对键名进行模糊查询
2020-08-19 15:34
-
php连接redis
2019-12-16 10:00
-
linux mshowfat命令显示MS-DOS文件在FAT...
2019-09-05 08:02
-
PHP中重定向网页跳转页面的方法
2019-07-09 11:38
-
CI框架中使用join实现多表联合查询
2019-06-13 20:56
-
Laravel 5.3 学习笔记之错误日志
2019-06-13 20:56
-
PHP批量生成静态HTML的简单原理和方法
2019-06-13 20:55
网友留言评论