正则替换
分享
55696
需求一:将手机号码中间四位替换成****
```
$phone = "15901234567";
echo preg
replace('/(\d{3})\d{4}(\d{4})/', "$1****$2", $phone);
```
需求二:去除字符串中的空格和换行符
```
$str = "hello world \n this is a test.";
echo preg
replace('/\s/', '', $str);
```
需求三:补全缺失的HTML标签
```
$html = "";
echo preg
replace('/(<(\w )>)/', '$1$2>', $html);
```
需求四:将字符串中的URL替换成超链接
```
$str = "这是一个网站:https://www.example.com";
echo preg
replace('/(https?:\/\/[\w\.\/] )/i', '$1', $str);
```
```
$phone = "15901234567";
echo preg
replace('/(\d{3})\d{4}(\d{4})/', "$1****$2", $phone);
```
需求二:去除字符串中的空格和换行符
```
$str = "hello world \n this is a test.";
echo preg
replace('/\s/', '', $str);
```
需求三:补全缺失的HTML标签
```
$html = "
这是一段文本
echo preg
replace('/(<(\w )>)/', '$1$2>', $html);
```
需求四:将字符串中的URL替换成超链接
```
$str = "这是一个网站:https://www.example.com";
echo preg
replace('/(https?:\/\/[\w\.\/] )/i', '$1', $str);
```