如何用星号替换某些字符

Sai*_*son 3 php

我有一个简单的任务与PHP,但由于我不熟悉正则表达式或其他东西......我不知道我将要做什么.

我想要的是非常简单的......

假设我有这些变量:

$Email = 'john@example.com'; // output : ****@example.com
$Email2 = 'janedoe@example.com'; // output : *******@example.com
$Email3 = 'johndoe2012@example.com'; // output : ***********@example.com

$Phone = '0821212121'; // output : 082121**** << REPLACE LAST FOUR DIGIT WITH *
Run Code Online (Sandbox Code Playgroud)

如何用PHP做到这一点?谢谢.

Mad*_*iha 16

你需要一个特定的功能.对于邮件:

function hide_mail($email) {
    $mail_segments = explode("@", $email);
    $mail_segments[0] = str_repeat("*", strlen($mail_segments[0]));

    return implode("@", $mail_segments);
}

echo hide_mail("example@gmail.com");
Run Code Online (Sandbox Code Playgroud)

对于电话号码

function hide_phone($phone) {
    return substr($phone, 0, -4) . "****";
}

echo hide_phone("1234567890");
Run Code Online (Sandbox Code Playgroud)

看到了吗?没有使用单个正则表达式.但这些功能并不检查有效性.您需要确定什么类型的字符串是什么,并调用适当的函数.