PHP以星号隐藏用户名手机和邮箱【原创

来源:https://www.sucaihuo.com/php/548.html 2015-12-29 10:06浏览(5155) 收藏

本文以淘宝评论购买记录中的隐藏部分用户名作为示例,并且拓展了隐藏手机号的中间四位数,和截取邮箱部分字符串,只要输入账号,可自动识别类型并以*显示
PHP以星号隐藏用户名手机和邮箱
分类:PHP > 函数 难易:入门级
下载资源 下载积分: 20 积分

隐藏函数

function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏
    if (strpos($str, '@')) {
        $email_array = explode("@", $str);
        $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
        $count = 0;
        $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
        $rs = $prevfix . $str;
    } else {
        $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
        if (preg_match($pattern, $str)) {
            $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4);
        } else {
            $rs = substr($str, 0, 3) . "***" . substr($str, -1);
        }
    }
    return $rs;
}

测试数据

<?php
$account = "sucaihuo.com";
$email = "416148489@qq.com";
$phone = "18005152525";
?>

以星号*显示实例

<table width="100%">
    <tr>
        <td>账号</td>
        <td>邮箱</td>
        <td>手机</td>
    </tr>
    <tr>
        <td><?php echo $account;?></td>
        <td><?php echo $email;?></td>
        <td><?php echo $phone;?></td>
    </tr>
    <tr>
        <td><?php echo hideStar($account);?></td>
        <td><?php echo hideStar($email);?></td>
        <td><?php echo hideStar($phone);?></td>
    </tr>
</table>
声明:本文为原创文章,如需转载,请注明来源sucaihuo.com并保留原文链接:https://www.sucaihuo.com/php/548.html
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2