Created
August 13, 2025 18:14
-
-
Save kpirnie/32ed1c28d31ceea0b05632680cb045fd to your computer and use it in GitHub Desktop.
PHP - Mask Email Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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( '@', '@', $_ret ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment