Php regular expression introduction _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php regular expression introduction. I have written some regular expression usage in this tutorial. it mainly describes the character segmentation, matching, search, replacement of regular expressions, and some basic knowledge and common examples of regular expressions, so I have written some regular expression usage in a tutorial today. it mainly describes the character segmentation, matching, search, replacement of regular expressions, and some basic knowledge and common examples of regular expressions, so I will sort it out and share it with you.

1. introduction and functions of regular expressions.

01. what is a regular expression?

Regular expressions (Regular Expression, regex, or regexp, abbreviated as RE) are also translated into Regular and Regular expressions. in Computer Science, it refers to a single string used to describe or match a series of strings that conform to a certain syntax rule. In many text editors or other tools, regular expressions are usually used to retrieve and/or replace text content that meets a certain pattern. Many programming languages support string operations using regular expressions.

Rule syntax

02. main functions: segmentation, matching, search, and replacement.

Expression Match

/^ S * $/

Matches empty rows.

/D {2}-d {5 }/

The ID number consists of two digits, one hyphen and five digits.

/ ] *)?> [SS] * /

Matches HTML tags.


2. two common regular functions in PHP.

The preg_match regular function is based on perl predictions. (More efficient. you need to customize a start Terminator .)
Ereg regular function, based on POSIX (Uniox, Script ).

3. elements contained in regular expressions.

01. atom (common character: a-z A-Z 0-9, atomic table, escape character ).
02. atomic character (character with special functions ).
03. pattern modifier (some built-in modules of the system, similar to functions ).


4. "Atom" in the regular expression ".

01. a-z A-Z _ 0-9 // The most common character.
02. (abc) (skd) // The unit symbol enclosed in parentheses.
03. [abcs] [^ abd] // greedy match. the Source table contained in square brackets. the atomic table ^ indicates exclusion or the opposite content.

04. escape characters (case sensitive)
D contains all numbers = [0-9].
D does not contain all numbers = [^ 0-9].

W contains all English characters = [a-zA-Z_0-9].
W contains not all English Characters & numbers, used to match special characters = [^ a-zA-Z_0-9].
S contains blank areas such as carriage return, line feed, and paging character = [fnr].

Metacharacters

* Match the previous content 0 times 1 time or multiple times
. Match content 0 times 1 time or multiple times, but does not contain carriage return line breaks
+ Match the previous content once or multiple times
? Matches the previous content 0 times or 1 time
| Select matching similar to in PHP | (because this operation conforms to the weak type, leading to the most overall match)
^ Match the first part of a string
$ Match string tail content
B matches the word boundary. the boundary can be a space or special match.
B. match the unexpected content except the word boundary.
{M} matches the previous content with M duplicates.
{M,} matches the repeat of the previous content more than or equal to M
{M, n} matches the number of repetitions of the previous content M to N
() Merge the overall match and put it into the memory. you can use 1 2... Obtain

Instance:

The code is as follows:

$ Mode = "# test #"; // The above atomic table can be used for matching.
$ Str = "sdfsstestdf ";

If (preg_match ($ mode, $ str, $ end) {// mode regular module, str regular content, and end regular result, which are output in an array.
Echo "matched successfully". $ end [0];
} Else {
Echo "matching failed ";
}
?>

Common regular expressions

* 1, ^ S + [a-z A-Z] $ cannot be blank cannot have spaces can only be English letters
* 2. S {6,} cannot be null for more than six characters
* 3. ^ d + $ cannot contain spaces or non-numbers.
* 4. (. * jpg (.jpg |. bmp) $ can only be in jpg and bmp formats.
* 5. ^ d {4}-d {1, 2}-d {1, 2} $ can only be in the format of 2004-10-22
* 6. select at least one item for ^ 0 $.
* 7. ^ 0 {2,} $ select at least two items.
* 8. ^ [s | S] {20,} $ cannot be empty for more than 20 words
* 9. ^ +? [A-z0-9] ([-+.] | [_] + )? [A-z0-9] +) * @ ([a-z0-9] + (. |-) + [a-z] {} $ email
* 10. w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w +) * ([,;] s * w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w +) * enter multiple addresses separated by commas (,) or spaces.
* 11. ^ ([0-9] + ))? [0-9] {87341628} $ a telephone number with a 7-or 8-digit phone number or a district code such as (022)
* 12, ^ [a-z A-Z 0-9 _] + @ [a-z A-Z 0-9 _] + (. [a-z A-Z 0-9 _] + (, [a-z A-Z 0-9 _] + @ [a-z A-Z
0-9 _] + (. [a-z A-Z 0-9 _] +) * $
* Only letters, numbers, and underscores are allowed. @ and. must be in the same format as a standard email.
* 13 ^ w + @ w + (. w +) + (, w + @ w + (. w +) * $ the above expression can also be written in this way, which is more refined.
14 ^ w + (-w +) | (. w +) * @ w + (. |-) w +) *. w + $ [/size]
Regular Expression Matching Chinese characters: [u4e00-u9fa5]


Match a specific number:

^ [1-9] d * $ // match a positive integer
^-[1-9] d * $ // match a negative integer
^ -? [1-9] d * $ // match the integer
^ [1-9] d * | 0 $ // match a non-negative integer (positive integer + 0)
^-[1-9] d * | 0 $ // match a non-positive integer (negative integer + 0)
^ [1-9] d *. d * | 0. d * [1-9] d * $ // match the positive floating point number
^-([1-9] d *. d * | 0. d * [1-9] d *) $ // match the negative floating point number
^ -? ([1-9] d *. d * | 0. d * [1-9] d * | 0 ?. 0 + | 0) $ // Match floating point number
^ [1-9] d *. d * | 0. d * [1-9] d * | 0 ?. 0 + | 0 $ // Match non-negative floating point number (positive floating point number + 0)
^ (-([1-9] d *. d * | 0. d * [1-9] d *) | 0 ?. 0 + | 0 $ // match a non-positive floating point number (negative floating point number + 0)

By letter ~ Z (case insensitive), numbers 0 ~ 9. combination of minus signs or underscores
The username must start with a number or letter and end with a length of 4 ~ 18 characters

The code is as follows:

^ [A-Za-z0-9] {1} [A-Za-z0-9 |-| _] {2-16} [A-Za-z0-9] {1} $

The username is an uppercase letter, lowercase letter, or underscore, and starts with a letter. the username must be 6-20 characters long.

The code is as follows:

^ [A-za-z] [wd _] {5, 19}

Username: The username can contain lowercase letters, Chinese characters, numbers, and underscores (_). The username cannot contain only numbers, but cannot contain underscores (_).

The code is as follows:

/^ [A-z0-9_u4e00-u9fa5] + [^ _] $/g

UTF-8

Preg_match ("/^ [a-z0-9_x80-xff] + [^ _] $/g", $ );

Under gbk:

Preg_match ("/^ [a-z0-9 _". chr (0xa1). "-". chr (0xff). "] + [^ _] $/", $)

Email

The code is as follows:

Function is_email ($ email ){
Return strlen ($ email)> 6 & preg_match ("/^ [w-.] + @ [w-] + (. w +) + $/", $ email );
}
?>

Url

The code is as follows:

Function autolink ($ foo)
{
$ Foo = eregi_replace ('(f | ht) {1} tp: //) [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/1', $ foo );
If (strpos ($ foo, "http") = FALSE ){
$ Foo = eregi_replace ('(www. [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/1', $ foo );
} Else {
$ Foo = eregi_replace ('([[: space:] () [{}]) (www. [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/123', $ foo );
}
Return $ foo;
}
?>

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.