<span id="Label3"></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">PHP supports two styles of regular expression syntax: POSIX and perl. Posix-style Regular expressions are easier to master, but not safe for binary mode, whereas perl-compatible regular expressions are relatively complex.</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">A regular expression is a string pattern that consists of ordinary characters (such as a~z) and special characters (called metacharacters). You can use regular expressions to do the following:</span><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;text-indent:2em;">① tests a pattern of a string, ② replaces text, and ③ extracts a substring from a string based on pattern matching. </span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">A Posix-style regular expression<br></span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">1. Writing Regular Expressions</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">Regular expressions are and metacharacters characters of ordinary characters, and the regular expressions of different meanings can be written by different combinations of meta-character and ordinary Character.</span></p></p><p style="text-indent:2em;text-align:center;"><p style="text-indent:2em;text-align:center;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">POSIX regular expression syntax format list</span></p></p><p style="text-indent:2em;text-align:center;"><p style="text-indent:2em;text-align:center;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;"></span></p></p> <table> <tbody> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Character</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Describe</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">An escape character used to escape a special character.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">^</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the starting position of the input string.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">$</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the end position of the input string.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">*</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the preceding subexpression 0 or more Times.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">+</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the preceding subexpression one or more times.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">?</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the preceding subexpression 0 or one Time.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">N</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">n is a non-negative integer that matches the determined N Times.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">{n,}</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">n is a non-negative integer that matches at least N times.</span></td> </tr> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">{n,m}</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Both M and N are non-negative integers, where n<=m matches at least N times and matches up to M times. Note There can be no spaces between the comma and two Numbers.</span></td> </tr> </tbody> </table><p style="text-indent:2em;text-align:center;"><p style="text-indent:2em;text-align:center;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;"></span></p></p> <table> <tbody> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">?</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">When the character immediately follows any other limiter (*,+,? , {n},{n,},{n,m}), the matching pattern is Non-greedy. The non-greedy pattern matches the searched string as little as possible, while the default greedy pattern matches as much of the searched string as Possible.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">.</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match any single character except "\ n" to match any character, including ' \ n ', using the ' [. \ n] ' Mode.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">(pattern)</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match pattern and get this Match. The obtained match is saved to the appropriate array. To match the parentheses character, use ' \ (' or ' \ ').</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">(?:p Attern)</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches pattern but does not get a matching result, which means that this is a non-fetch match and is not stored. This is in use ' or ' | ' is useful to combine parts of a pattern.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">(? =pattern)</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">forward-checking, the string that matches the pattern begins a matching lookup string. This is a non-fetch match, which means that the match does not need to be acquired for later Use. Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after a match, rather than starting with the character that contains the Pre-check.<br></span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">(?! Pattern</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">A negative Pre-Check matches the lookup string at the beginning of any string that does not match the Pattern. This is a non-fetch match, which means that the match does not need to be acquired for later Use. Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after a match, rather than starting with the character that contains the Pre-check.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">X|y</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match X or Y.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">[xyz]</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">The character set is Combined. Matches any one of the characters Contained.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">[^xyz]</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Negative values match the character set. Matches any character that is not contained.</span></td> </tr> <tr> <td width="105" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">[a-z]</span></td> <td width="586" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">The character Range. Matches any character within the established Range.</span></td> </tr> </tbody> </table> <table> <tbody> <tr> <td width="106" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">[^a-z]</span></td> <td width="585" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">A negative character range. Matches any character that is not in the specified range.</span></td> </tr> </tbody> </table><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">Note: It's a good idea to put regular expressions in single quotes in php.</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">2. Matching of strings</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">In php, functions that match posix-style regular expressions have the Ereg () and Eregi () functions.</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">You can use the Ereg () function to find the case where a string matches a substring, return the length of the matched string, and return an array of matched characters with the help of a parameter. Grammar:</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">int Ereg (string ($pattern), string $string [, array $reg])</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">The Ereg () function finds substrings in the string $string that match the given regular expression $pattern. In $pattern you can use parentheses "()" to enclose some sub-schemas to get this Match. If a substring that matches the $pattern in parentheses is found and the function calls the third parameter, $regs, the match is stored in the $regs array. $regs [1] contains the first left parenthesis to start the purple chuan, $regs [2] contains the second substring, and so On. $regs [0] Heroes match the whole string. If a match for the $pattern pattern is found in $string, the length of the matched string is returned, and false is returned if no match is found or an error occurs. This function returns 1 if the optional parameter $regs is not passed in, or if the matched string length is 0.</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The Eregi () function is basically the same as the Ereg () function, although the Ereg () function is case-sensitive and the Eregi () function is Case-insensitive.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">3. Substitution of strings</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Posix-style Regular expression functions The Ereg_replace () function, like the str_replace () function, can replace the found string with the specified string. The Ereg_replace () function enables more complex string Manipulation. Ereg_replace () function Syntax format:</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">String Ereg_replace (string $pattern, string $replacement, string $string)</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The function uses the string $replacement to replace the part of $pattern in the string $string and returns the replaced String. If no match is found, it is returned As-is.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">If $pattern contains a subset of parentheses, $replacement can contain substrings that are shaped like "\ \ $num", $num represent a number that will be replaced with a substring within the $num parentheses in the regular expression, which becomes a reverse reference.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The eregi_replacement () function is case Insensitive.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">4. Splitting an array</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">You can use the split () function to accomplish the same functions as the explode () function, and you can split the string according to the regular expression given, and return an array.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Array split (string $pattern, string $string [, int $limit])</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">This function uses $pattern as a boundary to split the string $string and to save the split substring back in the Array. If $limit is set, the returned array contains a maximum of $limit cells, and the last cell contains all the parts of the $string.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The Spliti () function is case Insensitive.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">5. Generating Regular Expressions</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Use the Sql_regcase () function to produce a case-insensitive regular expression.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">String Sql_regcase (string $string)</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The function returns a regular expression that matches the $string, not a case-sensitive letter. The returned expression is the conversion of each alphabetic character in $string to a square bracket expression that contains the case of the child Parent. Other characters remain Unchanged.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">second, perl-compatible Regular Expressions</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">1. Writing Regular Expressions</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">A perl-compatible regular expression must be included in the delimiter, which can be used as a delimiter in addition to any characters except numbers, captions, backslashes, and, If the delimiter is to appear in the expression, it needs to be escaped with an escape character.</p></p><p style="text-indent:2em;text-align:center;"><p style="text-indent:2em;text-align:center;"><span style="font-size:14px;">Syntax format for perl-compatible regular expression extensions</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"></p></p> <table> <tbody> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Character</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Describe</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\b</span></td> <td width="636" valign="top"><p><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a word boundary, which is the position between a word and a space.</span></p></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\b</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a Non-word boundary.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\cx</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches the control character indicated by X. The value of x must be one of a~z or a~z. otherwise, will ' C ' be a literal ' C ' character</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\d</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a numeric character.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\d</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a non-numeric character.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\f</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a page Break.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\ n</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a line Break.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\ r</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches a carriage return character.</span></td> </tr> <tr> <td width="55" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\s</span></td> <td width="636" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches any whitespace character, including spaces, tabs, page breaks, and so On.</span></td> </tr> </tbody> </table><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"></p></p> <table> <tbody> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\s</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches any non-whitespace Character.</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\ t</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match a tab</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\v</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match a vertical tab</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\w</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches any word character that includes an underscore.</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\w</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches any flying word character. Equivalent to [^a-za-z0-9]</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\xn</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Match n, where n is the hexadecimal escape value. The hexadecimal escape value must be two digits long for a determination. ASCII code can be used in regular expressions</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\num</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches num, where Num is a positive integer. A reference to the obtained Match.</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\ n</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Flag an octal escape value or a back reference. N is a back reference if there are at least N obtained sub-expressions before \ Nthe. otherwise, if n is the octal number (0~7), n is an octal escape value.</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\nm</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Flag an octal escape value or a back reference. If there are at least NM obtained subexpression before \nm, then NM is a back reference. If there are at least N fetches before \nm, then N is a back reference followed by the literal M. If both N and M are octal digits (0-7), the \nm will match the octal escape value nm If none of the preceding fields are Satisfied.</span></td> </tr> <tr> <td width="57" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\nml</span></td> <td width="634" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">If n is an octal number (0-3) and both M and L are octal digits (0-7), the octal escape value is matched NML</span></td> </tr> </tbody> </table> <table> <tbody> <tr> <td width="59" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">\un</span></td> <td width="632" valign="top"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;font-size:14px;">Matches n, where n is a Unicode character represented by 4 hexadecimal digits.</span></td> </tr> </tbody> </table><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">2. String Matching</span><br></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">Use the Preg_match () function to find the String. The syntax format is as Follows:</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;"><span style="font-family:‘微软雅黑‘, ‘Microsoft YaHei‘;">int Preg_match (string $pattern, string $subject [, array $matchs [, int $flags]])</span></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Searches the $subject string for content that matches the regular expression given by the $pattern. The Preg_match () function returns the number of times that $pattern Matches. Not 0 times is 1 times, because the Preg_match () function stops searching after the first Match.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">If the $matchs parameter is provided, the function is populated with the result of the Search. $matchs [0] will contain text that matches the entire pattern, $matchs [1] will contain the text that matches the Sub-pattern in the first captured parenthesis, and so On.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The $flags is an optional parameter in the Preg_relpace () function syntax format, and the value can be preg_offset_capture. If this tag is set, the matching result for each occurrence also returns its subordinate string Offset. This changes the value of the returned array so that each cell is also an array, where the first item is the matching string, and the second item is the Offset.<br></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Perl-compatible Regular Expressions also have a string-matching function, preg_match_all ().</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">int Preg_match_all (string $pattern, string $subject, array $matchs [, int $flags])</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The syntax of the function is the same as the Preg_match () function, which also searches for the specified string and places it in the corresponding array. The difference is that the Preg_match () function stops the match when it searches for the first matching result, and the Preg_match_all () function searches for the first match and then resumes the search at the end of the first match, knowing that the entire string is Searched. The value of the singular $flags of the preg_match_all () function can take the following three Types:<br></p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Ⅰpreg_pattern_order. default, which represents $matchs[0] is an array of all pattern matches, $match [1] is an array of strings that match the sub-patterns in the first parenthesis, and so On.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Ⅱpreg_set_order. If this tag is set, then $matches[0] is an array of the first set of matches, $matchs [1] is an array of the second set of occurrences, and so On.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Ⅲpreg_offset_capture. This tag can be combined with two other tags, and if this tag is set, the matching result for each occurrence also returns its subordinate string Offset.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">3. String substitution</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Use the Preg_replace () function to find a matching substring in a string and replace the substring with the specified string.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Search for $pattern pattern matches in $subject and replace with $replacement. If $limit is established, only $limit matches are replaced, and if $limit is omitted or the value is-1, all occurrences will be replaced.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">In the function syntax format, if $subject is an array, search and replace is performed for each item in the $subject, and a new one is Returned. If both $pattern and $replacement are arrays, the $preg_replace () function takes the value from $pattern in turn to search for and replace $subject.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">4. String Segmentation</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The Preg_split () function can use a regular expression as a boundary to split a string and return the string to an array, similar to the split () Function.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Array Preg_split (string $pattern, string $subject [, int $limit [, int $length]])</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">This function is case-sensitive and returns an array containing substrings that are split along a boundary that matches the $pattern in a $subject. The $limit is an optional parameter and, if specified, returns a maximum of $limit strings, if omitted or 1, there is no limit. The $flags value can be the following three types:</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Ⅰpreg_split_no_empty. If you set this tag, the function returns only Non-empty strings.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">ⅱpreg_split_delim_capture, If this tag is set, a match for the parentheses expression of the delimiter pattern is also captured and Returned.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Ⅲpreg_split_offset_capture. If you set this tag, the matching result for each occurrence also returns its subordinate string Offset.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">5. Returning a matched array cell</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Use the Preg_grep () function to find the specified array based on conditions and return the eligible array cells.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Array preg_grep (string $pattern, array $input [, int $flags])</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The $input function returns an array containing the array cells in the array that match $pattern. The $flags is an optional parameter, and the value can be preg_grep_invert, which means that cells in the input array that do not match a given $pattern are Returned.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">The array returned by the Preg_grep () function is indexed using the key name of the original array.</p></p><p style="text-indent:2em;text-align:left;"><p style="text-indent:2em;text-align:left;">Summary: in practical applications, the normal string function runs much faster than the regular expression Function. When working with simple strings, it is best to use a string function to accomplish This. When working with some complex strings, you can choose a regular expression function to do this when the string function does not solve the Problem. If you want to use regular expression functions, It is recommended that you use perl-compatible regular expression Functions.</p></p><p><p>This article is from the "rangers" blog, please be sure to keep this source http://ccnupxz.blog.51cto.com/8803964/1834855</p></p><p><p>Introduction to PHP Regular expressions</p></p></span>
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