search
Homephp教程php手册10个实用的PHP正则表达式

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

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

1. 验证E-mail地址 

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

Php代码 

  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. }  


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

Php代码 

  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. }  


2. 验证用户名 

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

Php代码 

  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. 验证电话号码 

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

Php代码 

  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地址的实例。 

Php代码 

  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. 验证邮政编码 

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

Php代码 

  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的实例。 

Php代码 

  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. 验证信用卡号 

Php代码 

  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. 验证域名 

Php代码 

  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中提取域名 

Php代码 

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


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

Php代码 

    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.   
    3. $text = preg_replace("/\b(regex)\b/i"'$text);  
    4.   
    5. echo $text;  
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!