PHP regular expression pattern matching example tutorial

WBOY
Release: 2016-07-25 08:51:41
Original
979 people have browsed it
  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 "

    "; </li>
    <li>print_r($mats); </li>
    <li>echo "
    ";
  6. ?>
  7. //match ip
  8. $str = "my ip is 192.168.10.1sdjlfajdf192 .178.39.4la";

  9. $ptn = '/d+.d+.d+.d+/';

  10. preg_match_all($ptn, $str,$mats);

  11. echo "

    "; </li>
    <li>print_r($mats); </li>
    <li>echo "
    ";

  12. ?>

Copy the code

pattern modifier and place it at the end of the regular expression i,m,s,u,e i: ignore case m: Treat as multiple lines s: treated as one line u: Greedy mode, maximum mode e: used when replacing, can be processed with functions, used to match the first parentheses in regular expressions

  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 "
    "; </li>
    <li>print_r($mats); </li>
    <li>echo "
    ";
  5. ?>
Copy code

mExample m is treated 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 "
    "; </li>
    <li>print_r($mats); </li>
    <li>echo "
    ";
$str = "Linux and php are lamp or nlinux is very much"; $ptn = '/.*/s'; preg_match_all($ptn, $str,$mats );

echo "
"; <div class="blockcode">print_r($mats); <div id="code_U7K">echo "
";
    ?>
  1. Copy code
  2. e usage
$str = "123 php"; $ptn = '/d+s(w+)/e';

$rep = 'strtoupper($1)';

// preg_match_all ($ptn, $str,$mats);
$str2 = preg_replace($ptn, $rep, $str);
echo "
"; <ol>print_r($str2); <li>echo "</pre&gt ;"; <li>?> </li>
<li>
<li>
<li>Copy code</li>
<li>
<li>
<li>backward reference
 </li>
<li>
<li>
<li>
</ol>
</div>
<em onclick="copycode($('code_AeR'));"><?php </em>$str = "123 php"; </div>$ptn = '/(d+)(s)(w+)/'; <p>$rep = '$3$2$1'; </p>// preg_match_all($ptn, $str,$mats); <div class="blockcode">$str2 = preg_replace($ptn, $rep, $str); <div id="code_lDd">echo "<pre class="brush:php;toolbar:false">"; <ol>print_r($str2); <li>echo "</ pre>"; </li>
<li>?> </li>
<li>
<li>
<li>Copy code</li>
<li>
<li>
<li>Five commonly used functions
1. String matching and replacement
preg_match();
preg_match_all();
preg_grep(); do a search

2. String replacement
preg_replace();

3. String segmentation
preg_split();
eval allows string expressions to be executed
Preg_grep example, search:
 </li>
<li>
<li>
<li>
</ol>
</div>
<em onclick="copycode($('code_lDd'));"><?php </em>//For example, representative article </div>$arr = array( <p>"php html", </p>" linux redhat rhce", <div class="blockcode">"junzaivip test php", <div id="code_Ee4">); <ol>//Need to search The content of <li>$ptn = '/junzaivip/'; </li>
<li>//Return the searched content </li>
<li>$arr2 = preg_grep($ptn, $arr); </li>
<li>echo "<pre class="brush:php;toolbar:false">"; </li>
<li>print_r($arr2); </li>
<li> echo "
";
  • ?>
  • Copy code
  • 4. Mathematical functions 1.max(); 2.min(); Note: 1. Multiple numbers, 2. Arrays composed of multiple numbers
  • echo max(3,45,6,7);
    echo "
    ";

    echo max(array(4,6,8,9));

    ? >
      Copy code
    1. 5.Date function 1.time(); 2.date(); //Convert timestamp to date 3.strtotime();//Convert date to timestamp 4.microtime();

      1. //calc Open the calculator
      2. The origin of time:
      3. echo time();
      4. echo "
        ";
      5. echo date("Y-m-d H:i-s w t", 0);
      6. ?>
      Copy code

      Convert time to timestamp

      1. cho strtotime("2014-12-12");
      2. ?>
      Copy code

      to calculate the specific date of the current time:

      1. echo date("Y-m-d H:i:s",time()+8*3600);
      2. ?>
      Copy code

      Find the current time by modifying the time zone date:

      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 the code

      Note: If each change is troublesome, just modify the php configuration file php.ini file directly, directly modify the date inside, find the timezone and change it to PRC date parameters: Y 2014 full year y In 2014, there were only the last two m 03 month has leading 0 n March has no leading 0 d 05 date has leading 0 j 5 date without leading 0 H 24 hours h 12 hours i 05 minutes s 05 seconds w 0-6 Sunday to Saturday t How many days are there in January 31 L Is it a leap year? //How to distinguish Pingrun years It is divisible by 4, and if it is divisible by 100, 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() Microseconds

      Calculate the running time of the script:

      1. $stime = microtime(1);//Note that this position must be true, otherwise it cannot participate in the calculation
      2. sleep(1);
      3. $etime = microtime(1);
      4. echo $etime - $stime;
      5. ?>
      Copy code

      Example: Perpetual calendar Perpetual calendar technical points 1. Year, month and day 2.Sunday to Saturday What day of the week is 3.1? 4.How many days are there in this month? 5. Next year and previous year 6.Next month and previous 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 the current year
      5. $year = $_GET['y']?$_GET['y']:date('Y');
      6. //Get the current month
      7. $month = $_GET['m' ]?$_GET['m']:date('m');
      8. //Get how many days there are in the current month
      9. $days = date('t',strtotime("{$year}-{$month}- 1"));//Double quotes must be used inside
      10. //What day of the week is the current first day?$weeks = date('w',strtotime("{$year}-{$month}-1"));
      11. //All content is centered
      12. echo "
        ";
      13. //Output header
      14. echo "

        {$year}year{$month}month

        ";
      15. //Output date Table
      16. echo "";
      17. //Output the first row
      18. echo "
      19. ";
      20. //The header cell is created by th
      21. echo "
      22. ";
      23. echo "
      24. ";
      25. echo "
      26. ";
      27. echo "
      28. ";
      29. echo "
      30. ";
      31. echo "
      32. ";
      33. echo "
      34. ";
      35. //Start laying out the form
      36. for($i = 1 - $weeks;$i <= $days;){
      37. echo "
      38. ";
      39. for ($j=0; $j < 7; $j++) {
      40. if ($i > $days || $i < 1) {
      41. echo "
      42. ";
      43. } else{
      44. echo "
      45. ";
      46. }
      47. $i++;
      48. }
      49. echo "
      50. ";
      51. }

      52. ";
      53. //Realize the previous year and the previous month
      54. if($month == 1){
      55. $prevyear = $year - 1;
      56. $prevmonth = 12;
      57. } else {
      58. $prevyear = $year;
      59. $prevmonth = $month -1;
      60. }
      61. if($month == 12){
      62. $nextyear = $year + 1;
      63. $nextmonth = 1;
      64. } else{
      65. $ nextyear = $year;
      66. $nextmonth = $month + 1;
      67. }

      68. //Buttons to output the previous month and the next month

      69. echo "

        Previous month|Next month

        ";
      70. echo "";
      71. ?>

      72. Copy code PHP error deal with 1. Turn off and on error reporting 2. Error reporting level 3. Error reporting place

        1), close and enable error reporting E_ALL E_ERROR //serious error E_WARNING //warning error E_PARSE//Syntax error E_NOTICE //Prompt error

        2), close error display_error = off What level of error is reported:

        error_reporting = E_ALL error_reporting = E_ALL & ~E_NOTICE //Report all errors except prompt errors

        3), where to report errors:

        //Whether to report an error from the browser display_error = off //Whether to output errors to a custom log file log_errors = on error_log = d:phplogsphp.log

      73. three four five sixecho "
        {$i}

      source:php.cn
      Statement of this Website
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template