Sample code for parsing PHP function preg_match () _php Tutorial

Source: Internet
Author: User
Tags function examples
php function Preg_match () prototype: int Preg_match (String $pattern, String $content [, array $matches])

The PHP function Preg_match () 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

 
 
  1. A matching string is required. The DATE function returns the current time
  2. $ content = "Current date and time is" .
    Date ("Y-m-d h:i a"). ",
  3. We are learning PHP together. ";
  4. Use the usual method to match the time
  5. if (Preg_match ("/d{4}-d{2}-d{2} d{2}
    :D {2} [ap]m/], $content, $m))
  6. {
  7. echo "matches the time:". $m [0]. "N";
  8. }
  9. Because the pattern of time is obvious, it can be easily matched.
  10. if (Preg_match ("/([d-]{10}) ([D:]{5}
    [ap]m)/", $content, $m))
  11. {
  12. echo "Current date is:" $m [1]. "N";
  13. echo "Current time is:". $m [2]. "N";
  14. }
  15. ?>

This is a simple dynamic text string matching instance. Assuming that the current system time is "August 17, 2006 13:25", the output will be as follows.
The time to match is: 2006-08-17 01:25 pm
The current date is: 2006-08-17
The current time is: 01:25 pm
The above is the PHP function Preg_match () of the specific function examples, I hope you can master.


http://www.bkjia.com/PHPjc/446252.html www.bkjia.com true http://www.bkjia.com/PHPjc/446252.html techarticle php function Preg_match () prototype: int Preg_match (String $pattern, String $content [, array $matches]) PHP function Preg_match () in $ The content string searches for regular expressions given with $pattern ...

  • 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.