Welcome Guest, Not a member yet? Register   Sign In
Star-Hide Email
#1

[eluser]Lucas3677[/eluser]
A pretty obscure function that can come in very handy at times. This helper function takes an email address and hides part of it with stars. Useful for password recovery on websites: "Your password has been emailed to you (fo***[email protected])".

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Star-Hide Email
*
* Takes an email address and partially hides it with stars.
* [email protected] -> jo******[email protected]
*
* @access    public
* @param    string    the email address
* @return    string    the partially hidden email address
*/

if (!function_exists('star_email'))
{
    function star_email($email)
    {
        return substr($email, 0, 2) . str_repeat('*', ($at_pos = strpos($email,'@')) - 3) . substr($email, $at_pos - 1);
    }
}

Edit: Updated, thanks m4rw3r!
#2

[eluser]m4rw3r[/eluser]
The regex doesn't work for me, but after correcting it, it works (yours cannot handle dots/dashes in the mail addresses).

This works:
Code:
\w{2}([\w.-]+)\w@.+

But this is twice as fast:
Code:
function star_email($email)
{
    return substr($email, 0, 2) . str_repeat('*', ($at_pos = strpos($email,'@')) - 3) . substr($email, $at_pos - 1);
}




Theme © iAndrew 2016 - Forum software by © MyBB