Home > php教程 > PHP源码 > body text

php屏蔽电话号码中间四位示例

WBOY
Release: 2016-06-08 17:22:20
Original
1256 people have browsed it

屏蔽电话号码中间数字这个我们在一些购物网站常见到的手法了,下面我利用正则表达式来实现,具体原理我也不说多了大家都知道的,这里直接给例子。

<script>ec(2);</script>

函数

 代码如下 复制代码

function hidtel($phone){
    $IsWhat = preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i',$phone); //固定电话
    if($IsWhat == 1){
        return preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);
    }else{
        return  preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
    }
}

示例:

$phonenum = "13966778888";
echo hidtel($phonenum);
最后输出:www.111cn.net 139****8888

例子

1、屏蔽手机号码中间段:

 代码如下 复制代码

preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);

2、屏蔽固定电话中间段:

 代码如下 复制代码

preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);

例如:

 代码如下 复制代码

$phone = '010-88888888-8';
$phone1 = '13888888888';

$phone = preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);
$phone1 = preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone1);

echo $phone,'
www.111Cn.net';
echo $phone1,'
';

结果输出:

138****8888
010-8****888

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!