http://www.iteye.com/news/23231

1. 验证E-mail地址

这是一个用于验证电子邮件的正则表达式。但它并不是高效、完美的解决方案。在此不推荐使用。

  1. $email = "test@ansoncheung.tk";
  2. if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
  3. echo "Your email is ok.";
  4. } else {
  5. echo "Wrong email address format";
  6. }
$email = "test@ansoncheung.tk";
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format";
}

为了更加有效验证电子邮件地址,推荐使用filer_var

  1. if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {
  2. echo "Your email is ok.";
  3. } else {
  4. echo "Wrong email address format.";
  5. }
if (filter_var('test+email@ansoncheung', FILTER_VALIDATE_EMAIL)) {
echo "Your email is ok.";
} else {
echo "Wrong email address format.";
}

2. 验证用户名

这是一个用于验证用户名的实例,其中包括字母、数字(A-Z,a-z,0-9)、下划线以及最低5个字符,最大20个字符。同时,也可以根据需要,对最小值和最大值做合理的修改。

  1. $username = "user_name12";
  2. if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {
  3. echo "Your username is ok.";
  4. } else {
  5. echo "Wrong username format.";
  6. }
$username = "user_name12";
if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) {
echo "Your username is ok.";
} else {
echo "Wrong username format.";
}

3. 验证电话号码

这是一个验证美国电话号码的实例。

  1. $phone = "(021)423-2323";
  2. if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x', $phone)) {
  3. echo "Your phone number is ok.";
  4. } else {
  5. echo "Wrong phone number.";
  6. }
$phone = "(021)423-2323";
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. $IP = "198.168.1.78";
  2. if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {
  3. echo "Your IP address is ok.";
  4. } else {
  5. echo "Wrong IP address.";
  6. }
$IP = "198.168.1.78";
if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)) {
echo "Your IP address is ok.";
} else {
echo "Wrong IP address.";
}

5. 验证邮政编码

这是一个用来验证邮政编码的实例。

  1. $zipcode = "12345-5434";
  2. if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
  3. echo "Your Zip code is ok.";
  4. } else {
  5. echo "Wrong Zip code.";
  6. }
$zipcode = "12345-5434";
if (preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
echo "Your Zip code is ok.";
} else {
echo "Wrong Zip code.";
}

6. 验证SSN(社会保险号)

这是一个验证美国SSN的实例。

  1. $ssn = "333-23-2329";
  2. if (preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$ssn)) {
  3. echo "Your SSN is ok.";
  4. } else {
  5. echo "Wrong SSN.";
  6. }
$ssn = "333-23-2329";
if (preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$ssn)) {
echo "Your SSN is ok.";
} else {
echo "Wrong SSN.";
}

7. 验证信用卡号

  1. $cc = "378282246310005";
  2. if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {
  3. echo "Your credit card number is ok.";
  4. } else {
  5. echo "Wrong credit card number.";
  6. }
$cc = "378282246310005";
if (preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc)) {
echo "Your credit card number is ok.";
} else {
echo "Wrong credit card number.";
}

8. 验证域名

  1. $url = "http://ansoncheung.tk/";
  2. if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
  3. echo "Your url is ok.";
  4. } else {
  5. echo "Wrong url.";
  6. }
$url = "http://ansoncheung.tk/";
if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}

9. 从特定URL中提取域名

  1. $url = "http://ansoncheung.tk/articles";
  2. preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
  3. $host = $matches[1];
  4. echo $host;
$url = "http://ansoncheung.tk/articles";
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1]; echo $host;

10. 将文中关键词高亮显示

  1. $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";
  2. $text = preg_replace("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);
  3. echo $text;
$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("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);

echo $text;

10个实用的PHP正则表达式 (转)的更多相关文章

  1. 10个实用的PHP正则表达式汇总

    原文 10个实用的PHP正则表达式汇总 正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本 ...

  2. 10个实用的PHP正则表达式

    正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...

  3. 10个实用的 CSS3 按钮效果制作教程

    人们往往喜欢那些有更多互动元素的网站,因此现在很多设计师专注于他们的 CSS3 技能.这是因为 CSS3 技能可以帮助他们在很大的程度上实现所需的吸引力.这里分享的10个优秀的 CSS3 按钮效果制作 ...

  4. ★10 个实用技巧,让Finder带你飞~

    10 个实用技巧,让 Finder 带你飞 Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组织和使用 ...

  5. ★10 个实用技巧,让Finder带你飞~

    10 个实用技巧,让 Finder 带你飞 Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组织和使用 ...

  6. 分享PHP中的10个实用函数

    分享PHP中的10个实用函数 PHP的功能越来越强大,里面有着非常丰富的内置函数.资深的PHP程序员对它们可能都很熟悉,但很多参加PHP培训的PHP初学者,仍然对一些非常有用的函数不太熟悉.这篇文章里 ...

  7. 绝对震撼 10个实用的jQuery/HTML5插件

    在HTML5的世界里,我们见证了无数的特效奇迹,但很多特效我们很难在网页中应用,今天我们要分享10款效果震撼但是又比较实用的jQuery/HTML5插件,希望这些项目在应用的过程中也能给你带来设计灵感 ...

  8. [转载].NET开发常用的10条实用代码

    1.读取操作系统和CLR的版本 OperatingSystem os = System.Environment.OSVersion; Console.WriteLine(“Platform: {0}” ...

  9. (转载)10个实用的但偏执的Java编程技术

    10个实用的但偏执的Java编程技术 在沉浸于编码一段时间以后(比如说我已经投入近20年左右的时间在程序上了),你会渐渐对这些东西习以为常.因为,你知道的…… 作者:小峰来源:码农网|2015-09- ...

随机推荐

  1. HDU 2602 Bone Collector (简单01背包)

    Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...

  2. iframe父子兄弟之间调用传值(contentWindow && parent)

    iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) 主页面调用iframe: iframe页面调用主页面: 主页面的包含的iframe之间相互调用: 主要知识点 ...

  3. Node.js缓冲模块Buffer

    前言 Javascript是为浏览器而设计的,能很好的处理unicode编码的字符串,但对于二进制或非unicode编码的数据就显得无能为力. Node.js继承Javascript的语言特性,同时又 ...

  4. Dom新find

    1.HTML标签和属性是不区分大小写的,但JS是区分大小写的:所以(1)HTML专有的接口的属性应该以小写字母开头,如果属性名由多个单词构成,第二个及接下来的每个单词的首字母都要大写.(2)有些HTM ...

  5. netbeans使用

    下载地址 https://netbeans.org/downloads/ https://netbeans.org/downloads/start.html?platform=linux&la ...

  6. JavaWeb项目开发案例精粹-第4章博客网站系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.fenye.Page; import com.sanqing ...

  7. HTML5塔防游戏——《三国塔防》 - Yorhom's Game Box

    h3{ font-size:20px; } HTML5塔防游戏--<三国塔防> 游戏介绍: <三国塔防>是一款基于HTML5和Javascript的2D塔防游戏.游戏中除了塔防 ...

  8. 使用struts taglib导致java.lang.NullPointerException: Module 'null' not found.

    比如说,只要jsp的代码里有<html:....>或者<bean:...>之类的struts标签就会在访问该jsp页面的时候报这个错 最后参考这篇文章,发现原来是因为web.x ...

  9. spring依赖注入单元测试:expected single matching bean but found 2

    异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Caused by: org.springframewor ...

  10. openfire插件开发入门1

    .案例插件的功能 这个插件很简单,就是在openfire Server启动时,和关闭时,在控制台打印出消息. 3.插件开发的目录结构设计 先来看一下当前openfire在eclipse中的目录结构: ...