PHP Regular Expression Preg_match () Usage _php tutorial

Source: Internet
Author: User
Tags php regular expression
With Preg_match (), we can complete the rule matching of strings. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. There is also an optional third parameter that allows you to put a matching part in an array. This functionality can become very useful when validating data.
The code is as follows Copy Code

The "I" after the pattern delimiter indicates a search with a case-insensitive letter

if (Preg_match ("/php/i", "PHP is the Web scripting language of choice.")) {

Print "A match was found.";

} else {

Print "A match is not found.";

}

?>

Get current time

copy code

"!--? php

//required matching string. The DATE function returns the current time. "Now moment: 2012-04-20 07:31 am"

$content = "now moment:". Date ("y-m-d h:i a");

//Match date and time.

If (Preg_match ("/d{4}-d{2}-d{2} d{2}:d {2} [ap]m/", $content, $m)

The time for

{

echo matches is: ". $m [0]. "N"; "2012-04-20 07:31 AM"

}

//Get date and time separately

if (Preg_match ("/([d-]{10}) ([d:]{5} [Ap]m]/", $content, $m))

{

echo ' Current date is: '. $m [1]. "N"; "2012-04-20"

echo "Current time is:". $m [2]. "N"; "07:31 am"

}

?


This example verifies that the email address is in the correct format. Now let's look at the various rules represented by this regular expression.

Get Google Home title
For example, to get the title of Google home page, the code is as follows:

The code is as follows Copy Code

$str = file_get_contents (' http://www.google.com ');
Preg_match ('/(.*)/', $STR, $arr);
echo $arr [1];
?>

Get domain name from URL

The code is as follows Copy Code
Preg_match ("/^" (HTTP//)? ( [^/]+)/I "," http://www.bKjia.c0m/index.html ", $matches);
$host = $matches [2]; Get the next two paragraphs from the hostname
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, $match is the result of the match.

If matches is provided, it is populated with the results of the search. $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the text that matches the sub-pattern in the first captured parenthesis, and so on.

$matches [0] will contain text that matches the entire pattern. Let's print out the first $matches with a pring_r:

The code is as follows Copy Code

Array (
[0] = http://www.bKjia.c0m
[1] =/http
[2] = = www.bKjia.c0m)


$matches [0] will contain text that matches the entire pattern, and $matches [1] will contain text that matches the child pattern in the first captured parenthesis. In the regular, () represents the pattern: matches pattern and obtains this match. The obtained matches can be obtained from the resulting Matches collection, the Submatches collection is used in VBScript, and the $0...$9 property is used in JScript. That is, the value in the array labeled 1 is the regular/^ (HTTP//)? ([^/]+)/I value in the first ()! The value of the array subscript 2 and so on.

http://www.bkjia.com/PHPjc/628982.html www.bkjia.com true http://www.bkjia.com/PHPjc/628982.html techarticle with Preg_match (), we can complete the rule matching of strings. If a match is found, the Preg_match () function returns 1, otherwise 0 is returned. There is also an optional third parameter that allows ...

  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.