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. Code 6.1 shows a code example for the Preg_match () function.
Code 6.1 date-time matching
The code is as follows:
<?php//String that needs to be matched. The date function returns $content = "Current date and Time was". Date ("y-m-d h:i a"). ", we are learning PHP together."; The usual method of matching time if (Preg_match ("//d{4}-/d{2}-/d{2}/d{2}:/d{2} [ap]m/", $content, $m)) {echo "matches the time is:". $m [0]. "/n"; }//Because the time pattern is obvious, it can also be easily matched if (Preg_match ("/([/d-]{10}) ([/d:]{5} [Ap]m]/", $content, $m)) {echo "The current date is:". $m [1]. "/n"; echo "Current time is:". $m [2]. "/n"; }?>
This is a simple dynamic text string matching instance. Assuming that the current system time is "August 17, 2016 13:25", the output will be as follows.
The time to match is: 2016-08-17 01:25 pm
The current date is: 2016-08-17
The current time is: 01:25 pm
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. For example, code 6.2 is a method for file name security checks.
Code 6.2 Security checks for file names
The code is as follows:
<?php $username = $_server[' Remote_user '); $filename = $_get[' file ']; Filter the file name to ensure system security if (!ereg (' ^[^./][^/]*$ ', $userfile)) {die (' This is not an illegal filename! '); }//Filter the user name if (!ereg (' ^[^./][^/]*$ ', $username)) {die (' This is not an invalid user name ')}//through security filtering, flattening the file path $thefile = "/home/$username/$f Ilename ";?>
Typically, using a Perl-compatible regular expression matching function, Perg_match (), will be faster than using Ereg () or eregi (). It is recommended to use the STRSTR () or Strpos () function if you are looking for a substring that contains only one string.
Substitution of regular expressions
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. The "/0" or "$ A" refers to 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. Code 6.6 is an applied example of this function, which demonstrates how to do a simple cleanup of the program source code.
Code 6.6 Cleanup of source code
The code is as follows:
<?php $lines = file (' source.php '); Reads the file into the array for ($i =0; $i <count ($lines); $i + +) {//Remove the comments that begin with "//" or "#" at the end of the line $lines [$i] = Eregi_replace ("(////|#). *$", "", $ lines[$i]); Eliminate the whitespace at the end of the line $lines [$i] = Eregi_replace ("[/n/r/t/v/f]*$", "/r/n", $lines [$i]); }//Collate output to page echo htmlspecialchars ("", $lines));?>
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. Code 6.7 is an application instance of an array substitution.
Code 6.7 Array Substitution
The code is as follows:
<?php//String $string = "Name: {name}<br>/nemail: {email}<br>/naddress: {address}<br>/n"; Mode $patterns =array ("/{address}/", "/{name}/", "/{email}/"); Replace string $replacements = Array ("No.5, Wilson St, New York, U.S.A", "Thomas Ching", "tom@emailaddress.com",); Output mode replacement result print preg_replace ($patterns, $replacements, $string);?>
The output results are as follows.
Name:thomas Ching ", Email:tom@emailaddress.com address:no.5, Wilson St., New York, U.S.A
The pattern modifier "E" can be used in preg_replace regular expressions. The effect is to use the match result as an expression, and it can be re-calculated. For example:
The code is as follows:
<?php $html _body = "
Tips
The Preg_replace function uses Perl-compatible regular expression syntax, which is often a faster alternative than ereg_replace. If you simply replace the string, you can use the Str_replace function.