Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Regex for email showing the first and last letters of the user and domain

I am new to regex and I am trying to substitute some characters for asterisk in php like below:

email: victor@gmail.com -> v****r@g***l.com

I have done this:

$email = preg_replace('/(?:^|@).\K|\.[^@]*$(*SKIP)(*F)|.(?=.*?\.)/', '*', $linha['cli_email']);

The problem is that the output is v*****@g****.com instead of v****r@g***l.com and I don't know how to show the last for the username and last for the email provider.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can try this:

 $email = preg_replace('/(?:^|.@).\K|.\.[^@]*$(*SKIP)(*F)|.(?=.*?\.)/', '*', $linha['cli_email']);

Hope it helped..

about 4 years ago · Santiago Trujillo Relatório

0

Here first of all we are matching string using regex.

Regex: (?<=\b[a-z])[a-zA-Z0-9]+(?=.\@)|(?<=@.)[a-zA-Z0-9]+(?=.\.)

(?<=\b[a-z])[a-zA-Z0-9]+(?=.\@) says match characters positive looking ahead for one character and @ and positive looking backward for boundary condition and a character.

(?<=@.)[a-zA-Z0-9]+(?=.\.) says match characters positive looking ahead for one character and . and positive looking backward for @ condition and a character.

Try this code snippet here

<?php

ini_set('display_errors', 1);
$email="victor@gmail.com";
preg_match_all('/(?<=\b[a-z])[a-z]+(?=.\@)|(?<=@.)[a-z]+(?=.\.)/',$email,$matches);
foreach($matches[0] as $key => $value)
{
    $email=str_replace($value, getStars(strlen($value)), $email);
}
echo $email;
function getStars($length)
{
    $string="";
    for($x=0;$x<$length;$x++)
    {
        $string.="*";
    }
    return $string;
}
about 4 years ago · Santiago Trujillo Relatório

0

You need to be accounting for all types of email address. How will the accepted answer handle email addresses like m@example.com or user@host.example.com or or joe@m.com?

<?php
function starmid($str) {
    switch (strlen($str)) {
        case 0: return false;
        case 1: return $str;
        case 2: return $str[0] . "*";
        default: return $str[0] . str_repeat("*", strlen($str) - 2) . substr($str, -1);
    }
}
function hideemail($email) {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        return false;
    }
    list($u, $d) = explode("@", $email);
    $d = explode(".", $d);
    $tld = array_pop($d);
    $d = implode(".", $d);
    return starmid($u) . "@" . starmid($d) . ".$tld";
}

$emails = [
    "victor@gmail.com",
    "v@example.com",
    "victor@g.com",
    "victor@host.example.com",
    "victor+foo@gmail.com",
    "invalid@",
];

foreach ($emails as $email) {
    echo hideemail($email) . "\n";
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda