Today's content
1. The regular form of the expression
2. Mathematical functions
3. Date function
4. Error handling
The form is:
1. Mode modifier
2. Five frequently used functions
Another regular form of the site: http://www.jb51.net/tools/zhengze.html
The regular form of the expression
1. Atoms
2. Meta-sub-characters
3. Mode modifier
The normal table-type function
1.preg_match ();
2.preg_match_all ();
3.preg_grep ();
4.preg_replace ();
5.preg_split ();
Atomic:
.: Represents a random character
\w: alpha-Numeric underline
Yenzi:
*: Retouch front, 0, 1, multiple. Represents random characters until the end
+: one. Multiple
?: 0 A front atom.
|: representative or
^: What's the opening?
$: At what end
\b: Word Edge
\b: Non-word edge
Single alpha-numeric
A-Z A-to 0-9 represents a random character
[] represents a random character inside
[^ABC] He's got a random character inside him except for ABC.
() represents a unit
\d a random number
\d Random a non-digital
\w: Represents a random alphanumeric underline
\w: Out of letters. Numbers. Underline an accidental random character
\s: whitespace characters
\s: A random character other than a blank character
{2}:2 an Atom
{2,}:2 more than one atom
{2,5}: 2-5 atoms in front
Mode modifier:
/Regular Table-U
Matches ordinary characters with the same name as the above special characters, which need to be added before/
<?php$sub = "www.baidu.com"; $ptn = '/\w*\.\w*\.\w*/';//the normal expression, metadata, returned data Preg_match ($PTN, $sub, $mats); echo " <pre> ";p rint_r ($mats); echo" </pre> ";? >
Matching IP
<?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 on the last surface of the regular table
I,m,s,u,e
I: Ignore uppercase and lowercase
M: As Multiple lines
S: as a row
U: Greedy mode, max mode
E: Used when replacing, able to be machined to match the first parenthesis in a regular form
<?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>";?>
Sample M
M as multiple lines
<?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 is 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 ($) ';//Preg_match_all ($PTN, $str, $mats); $str 2 = Preg_replace ($PTN, $rep, $STR); echo "<pre>"; Print_r ($str 2); echo "</pre>";?>
Backward reference
<?php $str = "123 php"; $ptn = '/(\d+) (\s) (\w+)/'; $rep = ' $3$2$1 ';//Preg_match_all ($PTN, $str, $mats); $str 2 = pr Eg_replace ($PTN, $rep, $STR); echo "<pre>"; Print_r ($str 2); echo "</pre>";?>
Five frequently used functions
1. Matching and substitution of strings
Preg_match ();
Preg_match_all ();
Preg_grep (); do a search
2. Substitution of strings
Preg_replace ();
3. Cutting of strings
Preg_split ();
eval allows string expressions to run
Preg_grep instances, do a search:
<?php //analogy represents the article $arr = array ( "php html", "Linux redhat rhce", "Junzaivip test php", ); what needs to be searched $ptn = '/junzaivip/'; Returns the contents of the search $arr 2 = Preg_grep ($PTN, $arr); echo "<pre>"; Print_r ($arr 2); echo "</pre>";?>
4. Mathematical Functions
1.max ();
2.min ();
Note: 1. Multiple numbers, 2, arrays of multiple numbers
<?PHP echo Max (3,45,6,7); echo "<br>"; Echo Max (Array (4,6,8,9));
>
5. Date function
1.time ();
2.date (); Convert Timestamp to date
3.strtotime ();//convert date to timestamp
4.microtime ();
Calc Open Calculator
Point of Origin of time:
<?php Echo Time (), echo "
Time conversion to timestamp
<?PHP echo strtotime ("2014-12-12");?>
Calculate the detailed date for the current time:
<?php echo Date ("Y-m-d h:i:s", Time () +8*3600);?>
To find the current date by changing the time zone:
<?php //Set the time zone in China to the default time zone Date_default_timezone_set ("PRC"); Echo Date ("Y-m-d h:i:s", when ());>
Note: If each change is more troublesome, go directly to change the PHP configuration file php.ini file, directly change the date inside to see TimeZone changed to PRC
Date parameter:
Y 2014 full
Y 14 only the latter two
M March has a leading 0
N March No preamble 0
d 05 Date has a leading 0
J 5th period no leading 0
H 24 hrs
H 12 hrs
I 05 minutes
s 05 seconds
W 0-6 Sunday to Saturday
T 311 months how many days
L is a leap year
How to differentiate Pinglunyan
Can be divisible by 4, at the same time assuming that it can be divisible by 100, it must be divisible by 400, when it is a leap year
<?php //Set China's time zone as the default time zone Date_default_timezone_set ("PRC"); $y = "1900/1/1"; $time = Strtotime ($y); Echo Date ("L", $ time);?>
Microtime () μs
To calculate the execution time of the script:
<?php $stime = microtime (1);//Note that this position must be true, whether the person cannot participate in the calculation of sleep (1), $etime = microtime (1); Echo $etime-$ stime;?>
Example: Perpetual calendar
Technical point of the perpetual calendar
1. A few months and a few days
2. Sunday to Saturday
Number No. 3.1 is the day of the week.
4. How many days are there this month?
5. Next year and previous year
6. Next month and last month
Perpetual Calendar Code:
<?PHP//change character encoding//header ("Content-type:text/html;charset=utf-8"); Date_default_timezone_set ("PRC"); Gets the current year $year = $_get[' y ']?$_get[' y ']:d ate (' Y '); Get Current Month $month = $_get[' m ']?
$_get[' m ']:d ate (' m '); Gets the number of days that the current month has been $days = date (' t ', Strtotime ("{$year}-{$month}-1")), or//the number of weeks $weeks = Date (' W ', Strtotime ("{$ year}-{$month}-1 ")); All have the content center echo "<center>"; Output header echo "
>
Error Handling in PHP
1. Closing and opening an error
2. Error Reporting level
3. Error Reporting Place
Turn off and turn on error
E_all
E_error Critical Error
E_warning//Warning error
E_parse//Syntax error
E_notice Prompt error
Close Error
Display_error = Off
Report what level of error:
error_reporting = E_all
error_reporting = E_all & ~e_notice//Report all errors, but in addition to prompt error
Error Place:
Whether to error from the browser
Display_error = Off
Whether to output the error to a self-defined log file
Log_errors = On
Error_log = D:\phplogs\php.log
Reprint Please specify source: HTTP://BLOG.CSDN.NET/JUNZAIVIP
PHP Nineth The use of the form in PHP