Home > Backend Development > PHP Tutorial > Detailed explanation of regular expression application in php

Detailed explanation of regular expression application in php

黄舟
Release: 2023-03-16 13:34:02
Original
1361 people have browsed it

Regular expression

1. Replace "/\d/", "#", $str: regular expression\d number, replace with #, string

$str = "2hello 5li 6lei";
echo preg_replace("/\d/","#",$str);
Copy after login

2 , Split the regular expression, string

$str = "2hello 5li 6lei";
var_dump(preg_split("/\d/",$str));
Copy after login

3. Match all strings that satisfy the regular expression

$str = "2hello 5li 6lei";
preg_match_all("/\d",$str,$arr);     //正则表达式,定义的字符串,数组
var_dump($arr);                            //匹配满足所有正则的字符串
Copy after login

4. Match the first string that satisfies the regular expression

$str = "2hello 5li 6lei";
preg_match("/\d/",$str,$arr);
var_dump($arr);
Copy after login

生成随机数
echo rand();
echo "<br>";
echo rand(0,10);
echo "<br>";
//获取当前时间
echo time();
echo "<br>";
//格式化显示时间
echo date("Y-m-d H:i:s","1506494423");
echo "<br>";
echo strtotime("2017-09-27 14:40:23");

字符串函数
去字符串长度
$str1="as";
$str2="abcdecdcd";
//比较长度ASC代码
var_dump(strcmp($str2,$str1));
//显示长度
echo strlen($str);
Copy after login
rrree

The above is the detailed content of Detailed explanation of regular expression application in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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