Php regular expression pattern matching example tutorial

Source: Internet
Author: User
Php regular expression pattern matching example tutorial

  1. $ Sub = "bbs.it-home.org ";

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

  3. // Regular expression, metadata, returned data

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

  5. Echo"

    ";  
  6. print_r($mats);
  7. echo "
  8. ";
  9. ?>
  10. // Match the ip address
  11. $ Str = "my ip is 192.168.10.1sdjlfajdf192.178.39.4la ";

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

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

  14. Echo"

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

  18. ?>

Pattern modifier, placed at the end of the regular expression I, m, s, u, ei: case-insensitive m: treated as multi-row s: treated as a row u: greedy mode, maximum mode e: used for replacement. it can be processed using a function to match the first parentheses 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. ?>

M example m is treated as multiple rows

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

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

Use of e

  1. $ Str = "123 php ";
  2. $ Ptn = '/\ d + \ s (\ w +)/E ';
  3. $ Rep = 'strtoupper ($1 )';
  4. // Preg_match_all ($ ptn, $ str, $ mats );
  5. $ Str2 = preg_replace ($ ptn, $ rep, $ str );
  6. Echo"
    ";  
  7. print_r($str2);
  8. echo "
  9. ";
  10. ?>

Back reference

  1. $ Str = "123 php ";
  2. $ Ptn = '/(\ d +) (\ s) (\ w + )/';
  3. $ Rep = '$3 $2 $1 ';
  4. // Preg_match_all ($ ptn, $ str, $ mats );
  5. $ Str2 = preg_replace ($ ptn, $ rep, $ str );
  6. Echo"
    ";  
  7. print_r($str2);
  8. echo "
  9. ";
  10. ?>

Five common functions 1. string match and replacement preg_match (); preg_match_all (); preg_grep (); search 2. string replacement preg_replace (); 3. string segmentation preg_split (); eval allows the string expression to execute the preg_grep instance for search:

  1. // For example, representing an article
  2. $ Arr = array (
  3. "Php html ",
  4. "Linux redhat rhce ",
  5. "Junzaivip test php ",
  6. );
  7. // Content to be searched
  8. $ Ptn = '/junzaivip /';
  9. // Return the searched content
  10. $ Arr2 = preg_grep ($ ptn, $ arr );
  11. Echo"
    ";  
  12. print_r($arr2);
  13. echo "
  14. ";
  15. ?>

4. mathematical functions 1.max(); 2.min(); Note: 1. an array composed of multiple numbers, 2, and multiple numbers

  1. Echo max (3,45, 6,7 );
  2. Echo"
    ";
  3. Echo max (array (4, 6, 8, 9 ));
  4. ?>

5. date functions 1. time (); 2. date (); // converts the timestamp to date 3. strtotime (); // converts a date to a timestamp 4. microtime ();

  1. // Open the calculator in calc.
  2. Time origin:
  3. Echo time ();
  4. Echo "";
  5. Echo date ("Y-m-d H: I-s w t", 0 );
  6. ?>

Time converted to timestamp

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

Calculate the specific date of the current time:

  1. Echo date ("Y-m-d H: I: Seconds", time () + 8*3600 );
  2. ?>

Modify the time zone to find the current date:

  1. // Set China's time zone to the default time zone
  2. Date_default_timezone_set ("PRC ");
  3. Echo date ("Y-m-d H: I: s", time ());
  4. ?>

Note: If every change is troublesome, you can directly modify the php configuration file. in the INI file, directly modify the date in it to see timezone and change it to the PRCdate parameter: y, March, all y, 14 years, only the last two m, May, the first 0 n, the second month, the second day 05 seconds w 0-6 Sunday to Saturday t 31 January how many days L whether it is a leap year // How to distinguish whether the normal year can be divided by four, at the same time, if it can be divisible by 100, it must be divisible by 400, which is a leap year.

  1. // Set China's time zone to 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. ?>

Microtime () microseconds

Run time of the computing script:

  1. $ Stime = microtime (1); // note that this location must be set to true. Otherwise, it cannot be calculated.
  2. Sleep (1 );
  3. $ Etime = microtime (1 );
  4. Echo $ etime-$ stime;
  5. ?>

Example: Perpetual Calendar technical point 1. A few months a few days 2. from Sunday to Saturday 3.1 is the day of the week. how many days are there in this month. next year and last year 6. calendar code for the next month and the last month:

  1. // Modify the character encoding

  2. // Header ("content-type: text/html; charset = utf-8 ");
  3. Date_default_timezone_set ("PRC ");
  4. // Obtain the current year
  5. $ Year = $ _ GET ['Y']? $ _ GET ['Y']: date ('Y ');
  6. // Obtain the current month
  7. $ Month = $ _ GET ['M']? $ _ GET ['M']: date ('M ');
  8. // Obtain the number of days in the current month.
  9. $ Days = date ('t', strtotime ("{$ year}-{$ month}-1"); // double quotation marks are required
  10. // Number one of the current days is the day of the week
  11. $ Weeks = date ('W', strtotime ("{$ year}-{$ month}-1 "));
  12. // Center all content
  13. Echo" ";
  14. // Output header
  15. Echo "{$ year}-{$ month}-month ";
  16. // Output date table
  17. Echo"
  18. // Output the first line
  19. // The header cell is created by th.
  20. Echo"
  21. Echo"
  22. Echo"
  23. Echo"
  24. Echo"
  25. Echo"
  26. Echo"
  27. // Start Table store
  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. "; "; "; "; "; "; "; "; "; "; "; "; "; ";
    DayIII3.ThuV.Sat.
    {$ I}
    ";
  44. // Implement the last year and the 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 the buttons for January 1, January and next January 1, January.

  60. Echo "last May January | next May January ";
  61. Echo"
  62. ";
  63. ?>

PHP error handling 1. disable and enable error reporting 2. Error Reporting Level 3. Error reporting location

1) close and enable error E_ALLE_ERROR // serious error E_WARNING // warning error E_PARSE // syntax error E_NOTICE // error prompt

2) when the display_error = off error is disabled, the following error levels are reported:

Error_reporting = E_ALLerror_reporting = E_ALL &~ E_NOTICE // all errors are reported, except for the error prompt

3) error:

// Whether to report display_error = off from the browser // whether to output the error 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.