The goal of the previous regular expression was to grab a bit of fiction from the Web, document AH, and get the video connection and download it in bulk. At that time, beginners PHP did not know that PHP has a special grasp of the tool, like simple_html_dom.php (mentioned in my other posts), before I did not know that there is this thing, so I waste their strength to learn the regular expression, Then learn how the function of the normal expression in PHP is used, and then analyze their own to crawl the DOM, the last to write their own regular expression, the regular expression, it is also possible to write, but they have just finished writing the regular expression may not see what he means.
There is a kind of ridicule of the regular expression, the regular expression is called the Martian text. When I first used simple_html_dom.php, this toolkit was a bit of a brief encounter bright. But fortunately, after all, I learned the regular expression, more learning something is still true. Writing regular expressions and then combining them with the normal expression functions of PHP is the advantage of being more flexible than using toolkits.
Here is the summary of the learning of regular expressions in PHP, and share with you, welcome to criticize
Regular expression functions in PHP
There are two sets of regular expression function libraries in PHP. The set is provided by the Pcre (Perl Compatible Regular Expression) library. The Pcre library uses the same syntax rules as Perl to implement pattern matching for regular expressions using functions named "Preg_". Another set is provided by the POSIX (Portable Operation System Interface) extension Library. The regular expression for POSIX extensions is defined by POSIX 1003.2, which generally uses a function named "Ereg_" as a prefix.
The functions of the two libraries are similar and the execution efficiency is slightly different. In general, the efficiency of using the PCRE library is slightly superior to achieving the same functionality. Here is a detailed description of how to use it.
Match the regular expression 1. Preg_match ()
Function prototype: int Preg_match (String $pattern, String $content [, array $matches])
The Preg_match () function searches the $content string for content that matches the regular expression given by the $pattern. If $matches is provided, the matching result is placed in it. $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the first captured content that matches the pattern cell in parentheses, and so on. This function only matches once, and eventually returns the number of matches for 0 or 1.
2. Ereg () and eregi ()
Ereg () is a matching function of the POSIX extended library expression. Eregi () is a case-insensitive version of the Ereg () function. The two functions are similar to Preg_match, but the function returns a Boolean value that indicates whether the match was successful or not. It is necessary to note that the first parameter of the POSIX extension library function accepts a regular expression string, that is, it does not require a delimiter.
3. Preg_grep ()
Function prototypes: Array preg_grep (string $pattern, array $input)
The Preg_grep () function returns an array that includes the cells in the $input array that match the given $pattern pattern. For each element in the input array $input, Preg_grep () also matches only once. The example given in code 6.3 simply illustrates the use of the Preg_grep () function.
Make a global regular expression match 1. Preg_match_all ()
Similar to the Preg_match () function. If a third parameter is used, all possible matching results are put in. This function returns the number of times the entire pattern matches (possibly 0) and returns False if an error occurs.
2. Multi-line matching
It is difficult to perform complex matching operations by using only regular table functions under POSIX. For example, the entire file, especially multiple lines of text, is matched to find. One way to do this with Ereg () is by branch processing.
Replacement of the regular expression 1. Ereg_replace () and Eregi_replace ()
Function Prototypes: String ereg_replace (String $pattern, String $replacement, String $string)
String Eregi_replace (String $pattern, String $replacement, String $string)
Ereg_replace () searches the $string for the pattern string $pattern and replaces the matched result with $replacement. When a pattern cell (or sub-mode) is included in a $pattern, the position of the $replacement in the form "\1" or "$" is replaced by the contents of those sub-patterns that are matched in turn. "+" or "$" means the contents of the entire matching string. It is important to note that the backslash is used as an escape character in double quotes, so you must use the form "\\0", "\\1".
Eregi_replace () and ereg_replace () are functionally consistent, except that the former ignores case.
2. Preg_replace ()
Function prototypes: Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
Preg_replace is more powerful than ereg_replace. The first three parameters can be used arrays, the fourth parameter $limit can set the number of substitutions, the default is to replace all.
The split of the regular expression 1. Split () and Spliti ()
Function prototype: Array split (String $pattern, string $string [, int $limit])
This function returns an array of strings, each of which is $string by a regular expression $pattern as a substring of the boundary. If $limit is set, the returned array contains a maximum of $limit cells. And the last unit contains all the rest of the $string. Spliti is the ignore size version of Split.
2. Preg_split ()
This function is consistent with the function of the split function.
A collection of functions for regular expressions in PHP