猿问

如何在 javascript 和 PHP 中获取部分电子邮件地址

我正在使用 TypeScript(Javascript)

我怎样才能像这样部分隐藏电子邮件地址?

jack.dawson@gmail.com至 j***** n@g.com

我正在使用下面的脚本来获得上面的输出。但是在@我需要得到三分之一的字符之后。例如:如果它是一个 10 个字符的字符串,我只需要显示三分之一的字符,其余的则被屏蔽。

let str = "tsadsadsadsadonkjhuhuhuy@gmail.com"

    str = str.split('');

    let finalArr = [];

    let len = str.indexOf('@');

    str.forEach((item, pos) => {

      (pos >= 3 && pos <= len - 2) ? finalArr.push('*'): finalArr.push(str[pos]);

    })


    document.write(finalArr.join(''));

    console.log(finalArr.join(''))

我也在尝试在 php 中执行此操作。我更喜欢 php 和 javascript 中的输出。

@我需要在符号之前得到三分之一的字符.com


大话西游666
浏览 144回答 3
3回答

ITMISS

我的简单 JavaScript 解决方案:let email = document.getElementById("email").innerHTML;//You should validate email format before proceedinglet parts = email.split("@");let name = parts[0];let tld = parts[1];let censored = name.charAt(0) + "****" + name.charAt(name.length-1)let result = censored + "@" + tld.substring(0, Math.floor(1/3*tld.length));document.getElementById("censored").innerHTML = result;<span id="email">thisismytestname@example.com</span><br><span id="censored"></span>PHP 解决方案工作方式基本相同:<?php$email = "myemail@test.com";//Should validate email format before proceeding$parts = explode("@", $email);$name = $parts[0];$tld = $parts[1];$censored = substr($name, 0, 1) . "****" . substr($name,-1);$result = $censored . "@" . substr($tld, 0, floor(1/3*strlen($tld)));echo $result; //echoes t****e@example.com?>

子衿沉夜

这是JS:const firstAndLast = str => {&nbsp; &nbsp; const firstChar = str.substring(0,3);&nbsp; &nbsp; const lastChar = str.slice(-1);&nbsp; &nbsp; return [firstChar, lastChar]}const firstThird = str => {&nbsp; &nbsp; const strArray = str.split(".");&nbsp; &nbsp; const result = []&nbsp; &nbsp; for (let i = 0; i < (strArray[0].length + 1)/3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; result.push(str[i])&nbsp; &nbsp; }&nbsp; &nbsp; return [result.join(''), strArray[1]]}const str = "tsadsadsadsajhuhuhuy@gmail.com"const strArray = str.split("@");const [userFirst, userLast] = firstAndLast(strArray[0])const [domainFirst, domainLast] = firstThird(strArray[1])console.log(`${userFirst}****${userLast}@${domainFirst}***.${domainLast}`)这是PHP:<?phpfunction firstAndLast($str) {&nbsp; &nbsp; $firstChar = substr($str, 0, 3);&nbsp; &nbsp; $lastChar = substr($str, -1);&nbsp; &nbsp; return [$firstChar, $lastChar];}function firstThird($str) {&nbsp; &nbsp; $strArray = explode(".", $str);&nbsp; &nbsp; $result = [];&nbsp; &nbsp; $str_length = (strlen($strArray[0]) + 1)/3;&nbsp; &nbsp; for ($i = 0; $i < $str_length; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($result, $str[$i]);&nbsp; &nbsp; }&nbsp; &nbsp; return [implode('', $result), $strArray[1]];}$str = "tsadsadsadsajhuhuhuy@gmail.com";$strArray = explode("@", $str);$user = firstAndLast($strArray[0]);$domain = firstThird($strArray[1]);echo $user[0].'****'.$user[1].'@'.$domain[0].'***.'.$domain[1];两者都将您的示例电子邮件变成:tsa****y@gm***.com

慕工程0101907

在PHP中用正则表达式提取中间部分:preg_match('\w(?<mid1>.*?)@\w(?<mid2>.*?)\.(?<ext>\w+)', $email, $matches)替换为字符串长度的 1/3str_replace($matches['mid1'], str_repeat('*'), strlen($matches['mid1']))工作示例
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答