Home > php教程 > php手册 > php手机号中间几位替换星号实例

php手机号中间几位替换星号实例

WBOY
Release: 2016-06-13 09:51:58
Original
1242 people have browsed it

今天小编来给各位同学介绍利用preg_replace函数把手机号码中间指定几伴替换成星号了,这个在很多网站 都会有这样做,下面我来介绍实例方法。


正则表达式方法

1、字符串中包含多个手机号码

 代码如下 复制代码

$s='王经理:13999312365 李经理:13588958741';
$s=preg_replace('#(d{3})d{5}(d{3})#', '${1}*****${2}', $s);
echo $s;
//王经理:139*****365 李经理:135*****741
?>

2、字符串中只有一个手机号码

 代码如下 复制代码

$haoma="15012345678";
echo preg_replace("/(d{3})d{5}/","$1*****",$haoma);
//150*****678
?>

不用正则表达式实现
1、使用substr_replace字符串部分替换函数

 代码如下 复制代码

$string1="13264309555";
echo substr_replace($string1,'*****',3,5);
//132*****555
?>

2、使用字符串截取函数substr

 代码如下 复制代码

echo substr($string1,0,3)."*****".substr($string1,8,3);
//132*****555
?>

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template