中间字符串以*隐藏

function t_companyname($company_name)
{
    $strlen = mb_strlen($company_name, 'utf-8');
    if ($strlen > 4) {
        $firstStr = mb_substr($company_name, 0, 3, 'utf-8');
        $lastStr  = mb_substr($company_name, -2, 3, 'utf-8');
        return 1 == $strlen ? $firstStr . str_repeat('*', mb_strlen($company_name, 'utf-8') - 2) : $firstStr . str_repeat("*", $strlen - 4) . $lastStr;
    } else {
        $firstStr = mb_substr($company_name, 0, 1, 'utf-8');
        $lastStr  = mb_substr($company_name, -1, 1, 'utf-8');
        return 2 == $strlen ? $firstStr . str_repeat('*', mb_strlen($company_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
    }
}

//用户名、邮箱、手机账号中间字符串以*隐藏
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})/';
        if (preg_match($pattern, $str)) {
            $rs = preg_replace($pattern, '$1****$2', $str); //手机号
        } else {
            $rs = substr($str, 0, 3) . "****" . substr($str, -1); //用户名
        }
    }
    return $rs;
}

你可能感兴趣的:(中间字符串以*隐藏)