How To Replace Some Characters With Asterisks

前端 未结 4 2058
北海茫月
北海茫月 2020-12-10 09:23

I have a simple task to do with PHP, but since I\'m not familiar with Regular Expression or something... I have no clue what I\'m going to do.

what I want is very si

4条回答
  •  不思量自难忘°
    2020-12-10 09:52

    For e-mails, this function preserves first letter:

    function hideEmail($email)
    {
        $parts = explode('@', $email);
        return substr($parts[0], 0, min(1, strlen($parts[0])-1)) . str_repeat('*', max(1, strlen($parts[0]) - 1)) . '@' . $parts[1];
    }
    
    hideEmail('hello@domain.com'); //  h****@domain.com
    hideEmail('hi@domain.com');    //  h*@domain.com
    hideEmail('h@domain.com');     //  *@domain.com
    

提交回复
热议问题