A
A
AndreyVitovtov2021-10-04 13:40:10
PHP
AndreyVitovtov, 2021-10-04 13:40:10

How to mask email using regular expression?

I need [email protected]to receive from u***[email protected]***l.com.uawhile I receive u***[email protected]***l.c***m.ua.

I found the code, but it's not exactly what I need:

function maskEmail($email)
{
    $email = preg_replace('/(?<=[\w]{1})[\w-\._\+%]+(?=[\w]{1}@)/', '***', $email);
    return preg_replace('/(?<=[\w]{1})[\w-\+%]+(?=[\w]{1}[.])/', '***', $email);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-10-04
@vabka

Try instead of regular expressions to use regular functions (not strong in php, therefore pseudocode)

функция замаскировать_адрес(адрес) {
  пусть [имя, домен] = адрес.разделить('@')
  пусть новое_имя = имя[0] + повторить('*', имя.длина-1)
  пусть новый_домен = повторить('*', домен.длина-2) + домен.подстрока(домен.длина-2, 2)
  вернуть новое_имя + '@' + новый_домен
}

A
Anton Shamanov, 2021-10-04
@SilenceOfWinter

no need to use a code whose meaning you do not understand - learn regular expressions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question