Learn the regular expression (3) - Check the E-mail address and other

xiaoxiao2021-03-06  71

Verify E-mail address

Let us then look at the E-mail address. An E-mail address consists of 3 parts:

POP3 username (the part on the left of '@'),

symbol'@',

Server Name (the remaining part)

A username may include: case case, numbers, junctions', minus' - ', underscore' _ '. And a server name is similar, except that it cannot contain underscores.

There is also the beginning, the beginning is not a period (have you seen the domain name of the date?), And you can't have two consecutive sentences, at least one other characters in them. Let's take a look at how to verify users. Part section:

^ [_ a-za-z0-9 -] $

This is still not allowed to change again:

^ [_ a-za-z0-9 -] (. [_ a-za-z0-9 -] * $

This represents at least one character, followed by any "single period, with more than 1 character".

For simple, we can use the following expressions on the EREGI () function (this function is not case-sensitive), we don't have to write all A-Z and A-Z, which is enough.

^ [_ a-z0-9 -] (. [_ a-z0-9 -] ) * $

The same is the same as the server name, just can't be underscore:

^ [A-Z0-9 -] (. [A-Z0-9 -] ) * $

Completed, put these two together, plus one '@' in the middle, we get:

^ [_ a-z0-9 -] (. [_ a-z0-9 -] ) * @ [a-z0-9 -] (. [A-Z0-9 -] ) * $

(Travel Note: If you carefully, you can think of the last string of the E-mail address server name 2 to 3 characters)

Other uses

Read the specific segment of the string

EREG () and EREGI () have a feature that can extract the required parts from the string (detailed reading manual), for example, read a file name from a path / URL:

EREG ("([^ //] *) $", $ PATHORURL, $ regs);

Echo $ regs [1];

Advancement replacement

EREG_REPLACE () and EREGI_REPLACE () are very useful. For example, we want to divide the words separated by commas from the string (using expLode is of course simple):

EREG_REPLACE ("[NRT] ", ",", TRIM ($ STR));

Some exercises (??? Have you made wrong)

Let you be busy:

Modify the expression of E-mail so that the server name consists of at least two parts (prompt: change a character)

Built a function using EREG_REPLACE () so that its function is similar to TRIM ()

Built a function using EREG_REPLACE (), replace the '#', '@', '&', and '%' in the string to '~'.

Transfer from: http://se2k.51.net/myphp/

转载请注明原文地址:https://www.9cbs.com/read-119624.html

New Post(0)