Home >Backend Development >PHP Tutorial >php regular expression application

php regular expression application

一个新手
一个新手Original
2017-09-29 10:29:131777browse

Regular expression

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

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

2 , Split the regular expression, string

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

3. Match all strings that satisfy the regular expression

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

4. Match the first string that satisfies the regular expression

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

Generate random numbers

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

字符串函数
去字符串长度
$str1="as";
$str2="abcdecdcd";
//比较长度ASC代码
var_dump(strcmp($str2,$str1));
//显示长度
echo strlen($str);
//拆分字符串
//split()--js
echo "0c6dc11e160d3b678d68754cc175188a";
var_dump(explode('b',$str2));
//链接字符
echo implode('|',["a","b","c"]);
替换字符串 查找替换
echo str_replace("cd","CD",$str2);
指定位置替换
echo substr_replace($str2,"xxx",0,3);
截取字符串
echo substr($str2,1,3);

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

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