Use of Regular Expressions in PHP in Lesson 9, regular expressions in PHP
Today's content
1. Regular Expression
2. mathematical functions
3. Date Functions
4. handle errors
Regular Expression:
1. Pattern Modifier
2. Five common functions
Another regular expression for the website: http://www.jb51.net/tools/zhengze.html
Regular Expression
1. Atom
2. Element sub-Character
3. Pattern Modifier
Regular Expression Functions
1. preg_match ();
2. preg_match_all ();
3. preg_grep ();
4. preg_replace ();
5. preg_split ();
Atom:
.: Represents any character
\ W: letter, digit, and underline
Child token:
*: Modify the first, 0, 1, multiple. represents any number of characters until the end
+: One or more
? : 0 previous Atoms
|: Represents or
^: Start with what
$: End
\ B: Word edge
\ B: Non-word edge
Single letter/Number
A-z A-Z 0-9 represents any character
[] Represents any character in it
[^ Abc] any character except abc
() Represents a Unit
\ D any number
\ D any non-digit
\ W: represents any letter, digit, or underscore
\ W: an unexpected character with letters, numbers, and underscores
\ S: white space characters
\ S: any character except the white space
{2}: 2 atoms
{2 ,}: more than two atoms
{2, 5}: 2-5 previous Atoms
Pattern modifier:
/Regular expression/U
To match common characters with the same name as the preceding special characters, add/
<? Php $ sub = "www.baidu.com"; $ ptn = '/\ w *\. \ w *\. \ w */'; // regular expression, metadata, returned data preg_match ($ ptn, $ sub, $ mats); echo "<pre>"; print_r ($ mats ); echo "</pre>" ;?>
// Match the ip address
<?php $str = "my ip is 192.168.10.1sdjlfajdf192.178.39.4la"; $ptn = '/\d+\.\d+\.\d+\.\d+/'; preg_match_all($ptn, $str,$mats); echo "<pre>"; print_r($mats); echo "</pre>"; ?>
Pattern modifier, placed at the end of the regular expression
I, m, s, u, e
I: case insensitive
M: multiple rows
S: regarded as a row
U: greedy mode, Max Mode
E: used for replacement. It can be processed using a function to match the first parentheses in a regular expression.
<?php $str = "Linux and php are lamp or linux is very much"; $ptn = '/linux/i'; preg_match_all($ptn, $str,$mats); echo "<pre>"; print_r($mats); echo "</pre>"; ?>
M example
M is considered as multi-row
<?php $str = "Linux and php are lamp or \nlinux is very much"; $ptn = '/^linux/im'; preg_match_all($ptn, $str,$mats); echo "<pre>"; print_r($mats); echo "</pre>"; ?>
Pattern Modifier
<?php $str = "Linux and php are lamp or \nlinux is very much"; $ptn = '/.*/s'; preg_match_all($ptn, $str,$mats); echo "<pre>"; print_r($mats); echo "</pre>"; ?>
Use of e
<?php $str = "123 php"; $ptn = '/\d+\s(\w+)/e'; $rep = 'strtoupper($1)'; // preg_match_all($ptn, $str,$mats); $str2 = preg_replace($ptn, $rep, $str); echo "<pre>"; print_r($str2); echo "</pre>"; ?>
Back Reference
<?php $str = "123 php"; $ptn = '/(\d+)(\s)(\w+)/'; $rep = '$3$2$1'; // preg_match_all($ptn, $str,$mats); $str2 = preg_replace($ptn, $rep, $str); echo "<pre>"; print_r($str2); echo "</pre>"; ?>
Five common functions
1. String Matching and replacement
Preg_match ();
Preg_match_all ();
Preg_grep (); Search
2. String replacement
Preg_replace ();
3. string segmentation
Preg_split ();
Eval enables string expression execution
Preg_grep instance for search:
<? Php // For example, representative Article $ arr = array ("php html", "linux redhat rhce", "junzaivip test php ",); // The content to be searched $ ptn = '/junzaivip/'; // return the searched content $ arr2 = preg_grep ($ ptn, $ arr ); echo "<pre>"; print_r ($ arr2); echo "</pre>";?>
4. mathematical functions
1. max ();
2. min ();
Note: 1. An array composed of multiple numbers, 2, and multiple numbers
<?php echo max(3,45,6,7); echo "<br>"; echo max(array(4,6,8,9)); ?>
5. Date Functions
1. time ();
2. date (); // converts a timestamp to a date.
3. strtotime (); // converts a date to a timestamp.
4. microtime ();
// Open the calculator in calc.
Time origin:
<?php echo time(); echo "
Time converted to Timestamp
<?php echo strtotime("2014-12-12"); ?>
Calculate the specific date of the current time:
<?php echo date("Y-m-d H:i:s",time()+8*3600); ?>
Modify the time zone to find the current date:
<? Php // set China's time zone to the default time zone date_default_timezone_set ("PRC"); echo date ("Y-m-d H: I: s", time ();?>
NOTE: If every change is troublesome, you can directly modify the php configuration file php. ini, and change the date in the file to change timezone to PRC.
Date parameter:
Y 2014
Y: only the last two digits in 14 years
M has a leading 0 in February
N no leading 0 in February
D 05 date with leading 0
J 5 date no leading 0
H 24 hours
H 12 hours
I 05 minutes
S 05 seconds
W 0-6 Sunday to Saturday
T 31, January days
L whether it is a leap year
// How to differentiate the normal year
It can be divisible by four, and if it can be divisible by 100, it must be divisible by 400. At this time, it is a leap year.
<? Php // set China's time zone to the default time zone date_default_timezone_set ("PRC"); $ y = "1900/1/1"; $ time = strtotime ($ y); echo date ("L ", $ time);?>
Microtime () microseconds
Run Time of the computing script:
<? Php $ stime = microtime (1); // note that this location must be set to true. Otherwise, sleep (1) cannot be calculated. $ etime = microtime (1 ); echo $ etime-$ stime;?>
Example: Perpetual Calendar
Perpetual calendar Technology
1. Several months and days
2. Sunday to Saturday
Number 3.1 is the day of the week
4. How many days are there in this month?
5. Next year and last year
6. Next May January and last May January
Perpetual calendar code:
<? Php // modify the character encoding // header ("content-type: text/html; charset = UTF-8"); date_default_timezone_set ("PRC "); // GET the current year $ year =$ _ GET ['y']? $ _ GET ['y']: date ('y'); // GET the current month $ month = $ _ GET ['M']? $ _ GET ['M']: date ('M'); // obtain the number of days in the current month. $ days = date ('T ', strtotime ("{$ year}-{$ month}-1"); // double quotation marks must be used in it. // The current number one is the day of the week $ weeks = date ('w ', strtotime ("{$ year}-{$ month}-1"); // all content centered echo "<center> "; // output table header echo "
PHP error handling
1. Close and enable Error Reporting
2. Error Report Level
3. Error Reporting location
Error Reporting for disabling and enabling
E_ALL
E_ERROR // serious error
E_WARNING // warning Error
E_PARSE // syntax error
E_NOTICE // error message
Close Error
Display_error = off
What level of error is reported:
Error_reporting = E_ALL
Error_reporting = E_ALL &~ E_NOTICE // all errors are reported, except for the error prompt
Error message:
// Whether to report an error from the browser
Display_error = off
// Whether to output the error to a custom log file
Log_errors = on
Error_log = d: \ phplogs \ php. log
Reprinted please indicate the source: http://blog.csdn.net/junzaivip
In php, the regular expression/^ [1-9] [0-9] * $/is
The first digit can only match nine numbers from 1 to 9. It can only match numbers from the second digit. In general, it matches all positive integers.
How does PHP use variables in a regular expression?
Int preg_match (string $ pattern, string $ subject [, array $ matches [, int $ flags])
String $ pattern is a string, so you can calculate the string and store it in a variable.
$ Star = 'a ';
$ Stop = 'C ';
$ Info = 'a1b2c3 ';
$ Pattern = '/'. $ star. '(. + ?) '. $ Stop .'/';
Preg_match ($ pattern, $ info, $ result );
Print_r ($ result [1]);