PHP Regular expression pattern matching example tutorial

Source: Internet
Author: User
Tags echo date php regular expression timestamp to date
    1. $sub = "bbs.it-home.org";

    2. $PTN = '/\w*\.\w*\.\w*/';

    3. Regular expressions, meta-data, returned data

    4. Preg_match ($PTN, $sub, $mats);

    5. echo "

      ";  
    6. Print_r ($mats);
    7. echo "
    8. ";
    9. ?>
    10. Matching IP
    11. $STR = "My IP is 192.168.10.1sdjlfajdf192.178.39.4la";

    12. $PTN = '/\d+\.\d+\.\d+\.\d+/';

    13. Preg_match_all ($PTN, $str, $mats);

    14. echo "

      ";  
    15. Print_r ($mats);
    16. echo "
    17. ";

    18. ?>

Copy Code

Pattern modifier, placed on the last side of the regular expression I,m,s,u,ei: Ignore case m: treated as multiple lines s: treated as one line u: Greedy mode, max mode e: Replace, can be processed by function, used to match the first parenthesis in a regular expression

    1. $STR = "Linux and PHP are lamp or Linux is very much";
    2. $PTN = '/linux/i ';
    3. Preg_match_all ($PTN, $str, $mats);
    4. echo "
      ";  
    5. Print_r ($mats);
    6. echo "
    7. ";
    8. ?>
Copy Code

m example m as multiple lines

    1. $str = "Linux and PHP are lamp or \nlinux is very much";
    2. $PTN = '/^linux/im ';
    3. Preg_match_all ($PTN, $str, $mats);
    4. echo "
      ";  
    5. Print_r ($mats);
    6. echo "
    7. ";
    8. ?>
Copy Code

pattern modifier

    1. $str = "Linux and PHP are lamp or \nlinux is very much";
    2. $PTN = '/.*/s ';
    3. Preg_match_all ($PTN, $str, $mats);
    4. echo "
      ";  
    5. Print_r ($mats);
    6. echo "
    7. ";
    8. ?>
Copy Code

Use of E

    1. $STR = "123 php";
    2. $PTN = '/\d+\s (\w+)/E ';
    3. $rep = ' Strtoupper ';
    4. Preg_match_all ($PTN, $str, $mats);
    5. $str 2 = preg_replace ($PTN, $rep, $STR);
    6. echo "
      ";  
    7. Print_r ($str 2);
    8. echo "
    9. ";
    10. ?>
Copy Code

Backward reference

    1. $STR = "123 php";
    2. $PTN = '/(\d+) (\s) (\w+)/';
    3. $rep = ' $3$2$1 ';
    4. Preg_match_all ($PTN, $str, $mats);
    5. $str 2 = preg_replace ($PTN, $rep, $STR);
    6. echo "
      ";  
    7. Print_r ($str 2);
    8. echo "
    9. ";
    10. ?>
Copy Code

Five common functions 1. Match and replace string Preg_match ();p reg_match_all ();p reg_grep (); Search 2. String substitution preg_replace (); 3. String segmentation preg_split (); eval allows a string expression to execute a Preg_grep instance and do a search:

    1. such as the Representative article
    2. $arr = Array (
    3. "PHP html",
    4. "Linux Redhat Rhce",
    5. "Junzaivip test PHP",
    6. );
    7. What you need to search for
    8. $PTN = '/junzaivip/';
    9. Go back to what you searched for
    10. $arr 2 = Preg_grep ($PTN, $arr);
    11. echo "
      ";  
    12. Print_r ($arr 2);
    13. echo "
    14. ";
    15. ?>
Copy Code

4. Mathematical function 1.max (); 2.min (); Note: 1. Multiple numbers, 2, arrays of multiple numbers

    1. echo Max (3,45,6,7);
    2. echo "
      ";
    3. echo Max (Array (4,6,8,9));
    4. ?>
Copy Code

5. Date function 1.time (); 2.date (); Convert the timestamp to date 3.strtotime ();//convert date to timestamp 4.microtime ();

    1. Calc Open Calculator
    2. Point of Origin of time:
    3. echo Time ();
    4. echo "";
    5. echo Date ("y-m-d h:i-s w T", 0);
    6. ?>
Copy Code

Time conversion to timestamp

    1. Cho strtotime ("2014-12-12");
    2. ?>
Copy Code

Calculate the exact date of the current time:

    1. echo Date ("Y-m-d h:i:s", Time () +8*3600);
    2. ?>
Copy Code

To find the current date by modifying the time zone:

    1. Set China's time zone as the default time zone
    2. Date_default_timezone_set ("PRC");
    3. echo Date ("Y-m-d h:i:s", Time ());
    4. ?>
Copy Code

Note: If each change is more troublesome, go directly to modify the PHP configuration file php.ini file, directly modify the inside of the date to see TimeZone modified to Prcdate parameters: Y 2014 full y 14 years only after two M March has a leading 0n March no leading 0d 05 Date has a leading 0j 5th period no leading 0H 24 h 12 h i 05 min s 05 sec w 0-6 Sunday to Saturday t 311 months how many days L is a leap year//How to distinguish the level is divisible by 4, and if it can be divisible by 100, then it must be divisible by 400, at this time it is a leap year

    1. Set China's time zone as the default time zone

    2. Date_default_timezone_set ("PRC");
    3. $y = "1900/1/1";
    4. $time = Strtotime ($y);

    5. echo Date ("L", $time);

    6. ?>
Copy Code

Microtime () μs

Calculate the run time of the script:

    1. $stime = Microtime (1);//Note This position must be true, no one can participate in the calculation
    2. Sleep (1);
    3. $etime = Microtime (1);
    4. Echo $etime-$stime;
    5. ?>
Copy Code

Example: Vannen Calendar Technical point 1. A few months and a few days 2. Sunday to Saturday No. 3.1 is the day of the Week 4. How many days are there in 5. Next year and previous year 6. Next month and last month perpetual calendar code:

  1. Modify character encoding

  2. Header ("Content-type:text/html;charset=utf-8");
  3. Date_default_timezone_set ("PRC");
  4. Get when the year before
  5. $year = $_get[' y ']?$_get[' y ']:d ate (' Y ');
  6. Get Current Month
  7. $month = $_get[' m ']?$_get[' m ':d ate (' m ');
  8. Gets the number of days that the current month is getting
  9. $days = date (' t ', Strtotime ("{$year}-{$month}-1"));//must be enclosed in double quotes
  10. The current number is a few weeks
  11. $weeks = Date (' W ', Strtotime ("{$year}-{$month}-1"));
  12. All have content centered
  13. echo " ";
  14. Output table Header
  15. echo "

    {$year} year {$month} month

    ";
  16. Output Date Table
  17. echo "
  18. Output First line
  19. Table header cells are created by th
  20. echo "
  21. echo "
  22. echo "
  23. echo "
  24. echo "
  25. echo "
  26. echo "
  27. Start Shop Form
  28. for ($j =0; $j < 7; $j + +) {
  29. } else{
  30. }
  31. }

  32. echo "
  33. echo "
  34. for ($i = 1-$weeks; $i <= $days;) {
  35. echo "
  36. if ($i > $days | | $i < 1) {
  37. echo "
  38. echo "
  39. $i + +;
  40. }
  41. echo "
  42. echo "

  43. "; "; "; "; "; "; "; "; "; "; "; "; "; ";
    Dayonetwothreefourfivesix
    {$i}
    ";
  44. Realize last year and last month
  45. if ($month = = 1) {
  46. $prevyear = $year-1;
  47. $prevmonth = 12;
  48. } else{
  49. $prevyear = $year;
  50. $prevmonth = $month-1;
  51. }
  52. if ($month = = 12) {
  53. $nextyear = $year + 1;
  54. $nextmonth = 1;
  55. } else{
  56. $nextyear = $year;
  57. $nextmonth = $month + 1;
  58. }

  59. Output buttons for the last month and next January

  60. echo "

    Last month | next month

    ";
  61. echo "
  62. ";
  63. ?>

Copy Code

Error handling in PHP 1. Closing and opening error 2. Bug report level 3. Error Reporting Place

1), turn off and turn on Error e_alle_error//Critical Error e_warning//Warning error e_parse//syntax error E_notice//Prompt error

2), off error Display_error = off what level of fault:

error_reporting = e_allerror_reporting = E_all & ~e_notice//Report all errors, but in addition to prompting for errors

3), Error Place:

Whether to error from the browser Display_error = off//whether to output errors to a custom log file Log_errors = Onerror_log = D:\phplogs\php.log
  • 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.