golang身份证邮箱手机号等等敏感信息加星

// 各种字符串加星

匹配 手机号,邮箱,中文,身份证等等

// 各种字符串加星
func HideStar(str string) (result string) {
    if str == "" {
        return "***"
    }
    if strings.Contains(str,"@") {
        res := strings.Split(str,"@")
        if len(res[0]) < 3 {
            resString := "***"
            result = resString + "@" + res[1]
        } else {
            res2 := Substr2(str,0,3)
            resString := res2 + "***"
            result = resString + "@" + res[1]
        }
        return result
    } else {
        reg := `^1[0-9]\d{9}$`
        rgx := regexp.MustCompile(reg)
        mobileMatch := rgx.MatchString(str)
        if mobileMatch {
            result =  Substr2(str,0,3) + "****" + Substr2(str,7,11)
        } else {
            nameRune := []rune(str)
            lens  := len(nameRune)

            if  lens <= 1 {
                result = "***"
            } else if lens == 2 {
                result = string(nameRune[:1]) + "*"
            } else if lens == 3 {
                result = string(nameRune[:1]) + "*" + string(nameRune[2:3])
            } else if lens == 4 {
                result =  string(nameRune[:1]) + "**" + string(nameRune[lens - 1 : lens])
            } else if lens > 4 {
                result =  string(nameRune[:2]) + "***" + string(nameRune[lens - 2 : lens])
            }
        }
        return
    }
}

func Substr2(str string, start int, end int) string {
    rs := []rune(str)
    return string(rs[start:end])
}

欢迎转载,但请附上原文地址哦,尊重原创,谢谢大家 本文地址: http://www.iphpt.com/detail/114/

当你能力不能满足你的野心的时候,你就该沉下心来学习