With preg_match (), we can complete string rule matching. If a match is found, the preg_match () function returns 1; otherwise, 0. Another optional third parameter allows you to store the matched part in an array. This function can be very useful when verifying data.
| The Code is as follows: |
Copy code |
|
<? Php
// The "I" after the pattern delimiter indicates a search that is case-insensitive.
If (preg_match ("/php/I", "PHP is the web scripting language of choice .")){
Print "A match was found .";
} Else {
Print "A match was not found .";
}
?> |
Get current time
| The Code is as follows: |
Copy code |
|
<? Php
// The string to be matched. The date function returns the current time. "Current Time: am"
$ Content = "current time:". date ("Y-m-d h: I ");
// Match the date and time.
If (preg_match ("/d {4}-d {2}-d {2} d {2}: d {2} [ap] m/", $ content, $ m ))
{
Echo "the matching time is:". $ m [0]. "n"; // "am"
}
// Obtain the date and time respectively.
If (preg_match ("/([d-] {10}) ([d:] {5} [ap] m)/", $ content, $ m ))
{
Echo "current date:". $ m [1]. "n"; // "2012-04-20"
Echo "the current time is:". $ m [2]. "n"; // "07:31 am"
}
?> |
This example verifies that the Email address is in the correct format. Now let's take a look at the various rules represented by this regular expression.
Get Google homepage title
For example, to obtain the title of the google homepage, the Code is as follows:
| The Code is as follows: |
Copy code |
|
<? Php
$ Str = file_get_contents ('HTTP: // www.google.com ');
Preg_match ('/<title> (. *) </title>/', $ str, $ arr );
Echo $ arr [1];
?> |
Retrieve domain name from website
| The Code is as follows: |
Copy code |
<? Php
Preg_match ("/^ (http ://)? ([^/] +)/I "," http://www.bKjia. c0m/index.html ", $ matches );
$ Host = $ matches [2]; // obtain the last two segments from the host name
Preg_match ("/[^./] +. [^./] + $/", $ host, $ matches );
Echo "domain name is: {$ matches [0]} n ";
?> |
Preg_match ($ pattern, $ string, $ matcher) where $ pattern corresponds to/^ (http ://)? ([^/] +)/I, $ string is http://www.php.net/index.html,$matchis the matching result.
If matches is provided, it is filled with the search results. $ Matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on.
$ Matches [0] will contain the text that matches the entire pattern. We use pring_r to print the first $ matches:
| The Code is as follows: |
Copy code |
|
Array (
Http://www.bKjia. c0m
[1] => http ://
[2] => www. bKjia. c0m) |
$ Matches [0] will contain the text that matches the entire pattern, and $ matches [1] will contain the text that matches the child pattern in the first captured parentheses. In the regular expression, () indicates the pattern: Match pattern and obtain this match. The obtained match can be obtained from the generated Matches set. The SubMatches set is used in VBScript, and $0… is used in JScript... $9 attribute. That is to say, the value marked as 1 in the array is/^ (http: //) in the regular expression ://)? ([^/] +)/I value in the first! The value of array subscript 2 is similar.