Chapter 4 data processing-php regular expression-zheng AQI (continued). For more information about learning php regular expressions, see. Php regular expressions are good applications.
Chapter 4 data processing-php regular expression-zheng AQI (continued). For more information about learning php regular expressions, see. Php regular expressions are good applications.
1. Basic knowledge of Regular Expressions
Meaning: String mode consisting of common characters (a-z) and special characters
Function: Validity verification.
Replace text.
Extract a substring from a string.
Classification: POSIX and Perl
POSIX is easier to master, but cannot be used in binary mode. perl is relatively complex.
2. POSIX Regular Expression
1. Write Regular Expressions
Table 4.3 POSIX regular expression syntax format list
Here are some examples of simple regular expressions:
● '[A-Za-z0-9]': indicates all uppercase letters, lowercase letters, and numbers ranging from 0 to 9.
● '^ Hello': A string starting with "hello.
● 'World $ ': A string ending with 'World.
● '. At': a string that starts with any single character except "\ n" and ends with "at", such as "cat" and "nat.
● '^ [A-zA-Z]': a string starting with a letter.
● 'Hi {2} ': indicates the letter h followed by two I, namely, hii.
● '(Go) +': indicates a string containing at least one 'Go' string, such as 'gogogo'
The ID card number is generally composed of 18 or 17 digits followed by an X or Y letter. to match the ID card number, you can write:
^ [0-9] {17} ([0-9] | X | Y) $
The Regular Expression of the Email address can be written as follows:
^ [A-zA-Z0-9 \-] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-\.] + $
2. String Matching
Ereg () and eregi () Functions
You can use the ereg () function to find matching conditions between a string and a sub-string, return the length of the matching string, and return an array of matching characters with parameters. The syntax format is as follows:
Int ereg (string ($ pattern), string $ string [, array $ regs])
The Code is as follows:
/* This example checks whether the string is a date in ISO format (YYYY-MM-DD )*/
$ Date = "1988-08-09 ";
$ Len = ereg ('([0-9] {4})-([0-9] {1, 2})-([0-9] {1, 2 })', $ date, $ regs); // The date format is YYYY-MM-DD
If ($ len)
{
Echo "$ regs [3]. $ regs [2]. $ regs [1]"."
"; // Output" 09.08.1988"
Echo $ regs [0]."
"; // Output" 1988-08-09"
Echo $ len; // output 10
}
Else
{
Echo "incorrect date format: $ date ";
}
?>
3. String replacement
The syntax of the ereg_replace () function is as follows:
String ereg_replace (string $ pattern, string $ replacement, string $ string)
Note: The function uses the string $ replacement to replace the $ string that matches $ pattern and returns the replaced string. If no match is found, return as is
The Code is as follows:
$ Str = "hello world ";
Echo ereg_replace ('[aeo]', 'x', $ str )."
"; // Output 'hxllx wxrld'
$ Res = 'hello ';
Echo ereg_replace ('hello', $ res, $ str); // replace 'hello' with a hyperlink'
?>
4. Split the Array
You can use the split () function to perform the same functions as the explode () function. You can also use the regular expression to split the string and return an array. The syntax format is as follows:
Array split (string $ pattern, string $ string [, int $ limit])
5. Generate a regular expression
3. Perl-Compatible Regular Expressions
1. Write Regular Expressions
Table 4.4 Perl is compatible with the syntax format expanded by regular expressions
2. String Matching
The preg_match () function searches strings. The syntax format is as follows:
Int preg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags])
Note: The structure of this function is similar to that of the ereg () function. In the $ subject string, search for the content that matches the regular expression given by $ pattern.
The preg_match () function returns the number of times $ pattern matches. If it is not 0 times (no match), it is 1 time, because the preg_match () function will stop searching after the first match.
Another one is preg_match_all (), which starts from the end of the first match until the complete string is searched.
The value of $ flags in the preg_match_all () function parameter can be as follows:
● PREG_PATTERN_ORDER. The default value is $ matches [0], which is an array that matches all modes,
$ Matches [1] is an array consisting of strings matching the child pattern in the first parentheses, and so on.
● PREG_SET_ORDER. If this tag is set, $ matches [0] is the array of the first group of matching items, $ matches [1] is the array of the second group of matching items, and so on.
● PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags,
If this flag is set, the offset of the affiliated string is also returned for each matching result.
3. String replacement
Use the preg_replace () function to perform the same functions as the ereg_replace () function, search for matched substrings In the strings, and replace the substrings with the specified strings.
The syntax format is as follows:
Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit])
4. string segmentation
The preg_split () function can use a regular expression as the boundary to split a string and store the substring into an array for return. This function is similar to the split () function.
The syntax format is as follows:
Array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags])
Note: This function is case sensitive and returns an array containing substrings separated by the boundary matching $ pattern in $ subject.
$ Limit is an optional parameter. If it is specified, a maximum of $ limit strings are returned. If it is omitted or-1, there is no limit.
The value of $ flags can be of the following three types:
● PREG_SPLIT_NO_EMPTY. If this flag is set, the function returns only non-null strings.
● PREG_SPLIT_DELIM_CAPTURE. If this mark is set, the matching items of the brackets expression in the delimiter mode will also be captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the offset of the affiliated string is also returned for each matching result.
4.3 instance-verify the Form Content
[Example 4.4] use a regular expression to verify whether the content of the form entered by the user meets the format requirements.
Create the EX4_4_Hpage.php file and enter the following code.
The Code is as follows:
Include 'ex4 _ 4_Hpage.php '; // contains the file EX4_4Hpage.php
$ Id = $ _ POST ['id'];
$ Pwd = $ _ POST ['pwd'];
$ Phone = $ _ POST ['phone'];
$ Email = $ _ POST ['email '];
$ Checkid = preg_match ('/^ \ w {} $/', $ id); // check whether the string is within 10 Characters
$ Checkpwd = preg_match ('/^ \ d {4, 14} $/', $ pwd); // check whether the value is 4 ~ Between 14 digits
$ Checkphone = preg_match ('/^ 1 \ d {10} $/', $ phone); // check whether it is an 11-digit number starting with 1
// Check the validity of the Email address
$ CheckEmail = preg_match ('/^ [a-zA-Z0-9 _ \-] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-\.] + $/', $ Email );
If ($ checkid & $ checkpwd & $ checkphone & $ checkEmail) // if both are 1, the registration is successful.
Echo "registration successful! ";
Else
Echo "registration failed, incorrect format ";
?>
Create the EX4_4_Ppage.php file and enter the following code:
2. String Matching
The preg_match () function searches strings. The syntax format is as follows:
Int preg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags])
Note: The structure of this function is similar to that of the ereg () function. In the $ subject string, search for the content that matches the regular expression given by $ pattern.
The preg_match () function returns the number of times $ pattern matches. If it is not 0 times (no match), it is 1 time, because the preg_match () function will stop searching after the first match.
Another one is preg_match_all (), which starts from the end of the first match until the complete string is searched.
The value of $ flags in the preg_match_all () function parameter can be as follows:
● PREG_PATTERN_ORDER. The default value is $ matches [0], which is an array that matches all modes,
$ Matches [1] is an array consisting of strings matching the child pattern in the first parentheses, and so on.
● PREG_SET_ORDER. If this tag is set, $ matches [0] is the array of the first group of matching items, $ matches [1] is the array of the second group of matching items, and so on.
● PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags,
If this flag is set, the offset of the affiliated string is also returned for each matching result.
3. String replacement
Use the preg_replace () function to perform the same functions as the ereg_replace () function, search for matched substrings In the strings, and replace the substrings with the specified strings.
The syntax format is as follows:
Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit])
4. string segmentation
The preg_split () function can use a regular expression as the boundary to split a string and store the substring into an array for return. This function is similar to the split () function.
The syntax format is as follows:
Array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags])
Note: This function is case sensitive and returns an array containing substrings separated by the boundary matching $ pattern in $ subject.
$ Limit is an optional parameter. If it is specified, a maximum of $ limit strings are returned. If it is omitted or-1, there is no limit.
The value of $ flags can be of the following three types:
● PREG_SPLIT_NO_EMPTY. If this flag is set, the function returns only non-null strings.
● PREG_SPLIT_DELIM_CAPTURE. If this mark is set, the matching items of the brackets expression in the delimiter mode will also be captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the offset of the affiliated string is also returned for each matching result.
4.3 instance-verify the Form Content
[Example 4.4] use a regular expression to verify whether the content of the form entered by the user meets the format requirements.
Create the EX4_4_Hpage.php file and enter the following code.
The Code is as follows:
Include 'ex4 _ 4_Hpage.php '; // contains the file EX4_4Hpage.php
$ Id = $ _ POST ['id'];
$ Pwd = $ _ POST ['pwd'];
$ Phone = $ _ POST ['phone'];
$ Email = $ _ POST ['email '];
$ Checkid = preg_match ('/^ \ w {} $/', $ id); // check whether the string is within 10 Characters
$ Checkpwd = preg_match ('/^ \ d {4, 14} $/', $ pwd); // check whether the value is 4 ~ Between 14 digits
$ Checkphone = preg_match ('/^ 1 \ d {10} $/', $ phone); // check whether it is an 11-digit number starting with 1
// Check the validity of the Email address
$ CheckEmail = preg_match ('/^ [a-zA-Z0-9 _ \-] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-\.] + $/', $ Email );
If ($ checkid & $ checkpwd & $ checkphone & $ checkEmail) // if both are 1, the registration is successful.
Echo "registration successful! ";
Else
Echo "registration failed, incorrect format ";
?>
Create the EX4_4_Ppage.php file and enter the following code:
The Code is as follows:
Include 'ex4 _ 4_Hpage.php '; // contains the file EX4_4Hpage.php
$ Id = $ _ POST ['id'];
$ Pwd = $ _ POST ['pwd'];
$ Phone = $ _ POST ['phone'];
$ Email = $ _ POST ['email '];
$ Checkid = preg_match ('/^ \ w {} $/', $ id); // check whether the string is within 10 Characters
$ Checkpwd = preg_match ('/^ \ d {} $/', $ pwd); // check whether the value is between 4-14 characters
$ Checkphone = preg_match ('/^ 1 \ d {10} $/', $ phone); // check whether the 11-digit child starts with 1
// Check the validity of the Email address
$ CheckEmail = preg_match ('/^ [a-zA-Z0-9 _ \-] + @ [a-zA-Z0-9 \-] + \. [a-zA-Z0-9 \-\.] + $/', $ Email );
If ($ checkid & $ checkpwd & $ checkphone & $ checkEmail) // if both are 1, the registration is successful.
Echo "registration successful! ";
Else
Echo "registration failed, incorrect format ";
?>