In the previous articles, we learned the use ofphp regular expressionsand getting started. Today we will focus on introducing thecommon functions of php regular expressions, both It can be used perfectly when combined! !
1. preg_match()
Function prototype:
int preg_match (string $pattern, string $content [, array $matches])
preg_match () function searches the $content string for content that matches the regular expression given by $pattern. If $matches is provided, the matching results are placed in it. $matches[0] will contain the text that matches the entire pattern, $matches[1] will contain the first captured match of the pattern element enclosed in parentheses, and so on. This function only performs one match and ultimately returns the number of matching results of 0 or 1. Listing 6.1 shows a code example for the preg_match() function.
Code 6.1 Date and time matching
The code is as follows:
This is a simple dynamic text string matching example. Assuming that the current system time is "13:25 on August 17, 2006", the following content will be output.
The matching time is: 2006-08-17 01:25 pm
The current date is: 2006-08-17
The current time is: 01:25 pm
2 . ereg() and eregi()
ereg() is the matching function for regular expressions in the POSIX extension library. eregi() is a case-ignoring version of the ereg() function. Both have similar functions to preg_match, but the function returns a Boolean value indicating whether the match was successful or not. It should be noted that the first parameter of the POSIX extension library function accepts a regular expression string, that is, no delimiter is required. For example, Listing 6.2 is a method for checking the security of file names.
Code 6.2 Security check of file name
The code is as follows:
##
1. ereg_replace() and eregi_replace()
string ereg_replace (string $pattern, string $replacement, string $string) string eregi_replace (string $pattern, string $replacement, string $string)
eregi_replace() and ereg_replace() have the same functions, except that the former ignores case. Code 6.6 is an application example of this function. This code demonstrates how to do simple cleaning work on the program source code.
Code 6.6 Cleaning up the source code
The code is as follows:
2. preg_replace()
mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
Code 6.7 Array replacement
The code is as follows:
/nEmail: {Email}
/nAddress: {Address}
/n"; //模式 $patterns =array( "/{Address}/", "/{Name}/", "/{Email}/" ); //替换字串 $replacements = array ( "No.5, Wilson St., New York, U.S.A", "Thomas Ching", "tom@emailaddress.com", ); //输出模式替换结果 print preg_replace($patterns, $replacements, $string); ?>
Name: Thomas Ching", Email: tom@emailaddress.com Address: No.5, Wilson St., New York, U.S.A
The code is as follows:
TEST
My Picture