Skip to content

Instantly share code, notes, and snippets.

@kpirnie
Created August 13, 2025 18:14
Show Gist options
  • Save kpirnie/32ed1c28d31ceea0b05632680cb045fd to your computer and use it in GitHub Desktop.
Save kpirnie/32ed1c28d31ceea0b05632680cb045fd to your computer and use it in GitHub Desktop.
PHP - Mask Email Address
/**
* mask_email_address
*
* Mask an email address from bots
*
* @since 7.3
* @access public
* @static
* @author Kevin Pirnie <[email protected]>
* @package Kevin's CRM and Support
*
* @param string $_value The email address to mask
*
* @return string The masked email address
*
*/
public static function mask_email_address( string $_value ) : string {
// setup our return string
$_ret = '';
// loop over the characters in the passed in value
for ( $i = 0, $len = strlen( $_value ); $i < $len; $i++ ) {
// setup a random character
$j = rand( 0, 1 );
if ( 0 == $j ) {
$_ret .= '&#' . ord( $_value[ $i ] ) . ';';
} elseif ( 1 == $j ) {
$_ret .= $_value[ $i ];
} elseif ( 2 == $j ) {
$_ret .= '%' . zeroise( dechex( ord( $_value[ $i ] ) ), 2 );
}
}
// return our reformatted string
return str_replace( '@', '&#64;', $_ret );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment