Home Backend Development PHP Tutorial PHP regular expression verification (email address, Url address, phone number, zip code)

PHP regular expression verification (email address, Url address, phone number, zip code)

Jul 29, 2016 am 08:58 AM
com function return

The content that needs to be verified in the example of this article: email address, URL address, phone number, postal code. The verification method is shared with everyone for your reference. The specific content is as follows

1. Verification of email address

<&#63;php
/* 校验邮件地址*/
function checkMail($email) {
//用户名,由“\w”格式字符、“-”或“.”组成
$email_name= "\w|(\w[-.\w]*\w)";
//域名中的第一段,规则和用户名类似,不包括点号“.”
$code_at= "@";
$per_domain= "\w|(\w[-\w]*\w)";
//域名中间的部分,至多两段
$mid_domain= "(\." .$per_domain. "){0,2}";
//域名的最后一段,只能为“.com”、“.org”或“.net”
$end_domain= "(\.(com|net|org))";
$rs= preg_match(
"/^{$email_name}@{$per_domain}{$mid_domain}{$end_domain}$/",
$email
);
return (bool)$rs;
}
//测试,下面均返回成功
var_dump( checkMail("root@localhost") );
var_dump( checkMail("Frank.Roulan@esun.edu.org") );
var_dump( checkMail("Tom.024-1234@x-power_1980.mail-address.com") );
&#63;>
Copy after login

2. URL address verification

<&#63;php
/* 校验URL地址*/
function checkDomain($domain)
{
return ereg("^(http|ftp)s&#63; ://(www\.)&#63;.+(com|net|org)$", $domain);
}
$rs= checkDomain("www.taodoor.com");//返回假
$rs= checkDomain("http://www.taodoor.com");//返回真
&#63;>
Copy after login

3, phone number

<&#63;php
/* 校验电话号码*/
function checkTelno($tel)
{
//去掉多余的分隔符
$tel= ereg_replace("[\(\)\. -]", "", $tel);
//仅包含数字,至少应为一个6位的电话号(即没有区号)
if(ereg("^\d+$", $tel))
{
return true;
}else{
return false;
}
}
$rs= checkTelno("(086)-0411-12345678");//返回真
&#63;>
Copy after login

4, postal code verification

<&#63;php
/* 校验邮政编码*/
function checkZipcode($code)
{
//去掉多余的分隔符
$code = preg_replace("/[\. -]/", "", $code);
//包含一个6位的邮政编码
if(preg_match("/^\d{6}$/", $code))
{
return true;
}else{
return false;
}
}
$rs= checkZipCode("123456");//返回真
&#63;>
Copy after login

I hope this article will be helpful to everyone learning PHP programming.

The above introduces PHP regular expression verification (email address, Url address, phone number, postal code), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

What is the difference between comcn and com What is the difference between comcn and com May 12, 2023 pm 04:08 PM

The difference between comcn and com: 1. There are differences between comcn and com in terms of meaning, but there is no difference in access speed; 2. comcn is an international domain name and is a global top-level domain name for use by commercial institutions, while cn is a Chinese company domain name , domestic commercial institutions, domestic domain names, only enterprises can register; 3. The search priority is that cn will search for .cn first. After finding the .cn server, the .cn server will then search for .com; 4. cn is provided by cnnic China Internet Center Management, com's management organization is abroad.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

How does Vue3 use setup syntax sugar to refuse to write return How does Vue3 use setup syntax sugar to refuse to write return May 12, 2023 pm 06:34 PM

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

See all articles