Home Backend Development PHP Tutorial php regular expression application

php regular expression application

Sep 29, 2017 am 10:29 AM
php regular expression

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

Generate random numbers

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);
//拆分字符串
//split()--js
echo "<br>";
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);
Copy after login

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles