PHP regular expression pattern matching example tutorial
WBOYOriginal
2016-07-25 08:51:411015browse
$sub = "bbs.it-home.org";
$ptn = '/w*.w*.w */';
// Regular expression, metadata, returned data
preg_match($ptn, $sub,$mats);
echo "
"; </li>
<li>print_r($mats); </li>
<li>echo "
";
?>
//match ip
$str = "my ip is 192.168.10.1sdjlfajdf192 .178.39.4la";
$ptn = '/d+.d+.d+.d+/';
preg_match_all($ptn, $str,$mats);
echo "
"; </li>
<li>print_r($mats); </li>
<li>echo "
";
?>
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
$str = "Linux and php are lamp or linux is very much";
$ptn = '/linux/i';
preg_match_all($ptn, $str,$mats) ;
echo "
"; </li>
<li>print_r($mats); </li>
<li>echo "
";
?>
Copy code
mExample
m is treated as multiple lines
$str = "Linux and php are lamp or nlinux is very much";
$ptn = '/^linux/im';
preg_match_all($ptn, $str,$mats
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 "
5.Date function
1.time();
2.date(); //Convert timestamp to date
3.strtotime();//Convert date to timestamp
4.microtime();
//calc Open the calculator
The origin of time:
echo time();
echo "";
echo date("Y-m-d H:i-s w t", 0);
?>
Copy code
Convert time to timestamp
cho strtotime("2014-12-12");
?>
Copy code
to calculate the specific date of the current time:
echo date("Y-m-d H:i:s",time()+8*3600);
?>
Copy code
Find the current time by modifying the time zone date:
//Set China’s time zone as the default time zone
date_default_timezone_set("PRC");
echo date("Y-m-d H:i:s",time());
?>
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.
//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);
?>
Copy code
microtime() Microseconds
Calculate the running time of the script:
$stime = microtime(1);//Note that this position must be true, otherwise it cannot participate in the calculation
sleep(1);
$etime = microtime(1);
echo $etime - $stime;
?>
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:
//Modify 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');
//Get how many days there are in the current month
$days = date('t',strtotime("{$year}-{$month}- 1"));//Double quotes must be used inside
//What day of the week is the current first day?$weeks = date('w',strtotime("{$year}-{$month}-1"));
//All content is centered
echo "
";
//Output header
echo "
{$year}year{$month}month
";
//Output date Table
echo "
";
//Output the first row
echo "
";
//The header cell is created by th
echo "
日
";
echo "
一
";
echo "
二
";
echo "
three
";
echo "
four
";
echo "
five
";
echo "
six
echo "
";
//Start laying out the form
for($i = 1 - $weeks;$i <= $days;){
echo "
";
for ($j=0; $j < 7; $j++) {
if ($i > $days || $i < 1) {
echo "
";
} else{
echo "
{$i}
";
}
$i++;
}
echo "
";
}
";
//Realize the previous year and the previous month
if($month == 1){
$prevyear = $year - 1;
$prevmonth = 12;
} else {
$prevyear = $year;
$prevmonth = $month -1;
}
if($month == 12){
$nextyear = $year + 1;
$nextmonth = 1;
} else{
$ nextyear = $year;
$nextmonth = $month + 1;
}
//Buttons to output the previous month and the next month
//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
Statement:
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