設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 744|回復: 0
打印 上一主題 下一主題

10个实用的PHP正则表达式

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2016-1-17 10:55:37 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
10-useful-php-regular-expression
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. }
複製代碼
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. }
複製代碼
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. }
複製代碼
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. }
複製代碼
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. }
複製代碼
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. }
複製代碼
 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. }
複製代碼
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. }
複製代碼
9. 从特定URL中提取域名
  1. $url = "http://ansoncheung.tk/articles";
  2. preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
  3. $host = $matches[1];
  4. 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;
複製代碼











分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-15 23:11 , Processed in 0.278057 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表