Skip to content

Instantly share code, notes, and snippets.

@shardiwal
Last active August 29, 2015 14:10
Show Gist options
  • Save shardiwal/a9f7b2d1d2b3ca6aa16e to your computer and use it in GitHub Desktop.
Save shardiwal/a9f7b2d1d2b3ca6aa16e to your computer and use it in GitHub Desktop.
sanitize email.php
function sanitize_email( $content ) {
$data = array();
foreach ( ( explode(' ',$content) ) as $address) {
$email = filter_var($address,FILTER_VALIDATE_EMAIL);
if ( $email ) {
$email = preg_replace('/\./', '[dot]', $email);
$email = preg_replace('/\@/', '[at]', $email);
$data[] = $email;
}
else {
$data[] = $address;
}
}
return implode(' ',$data);
};
function sanitize_email_in_html( $content ) {
$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,3})?(\.[a-z]{2})?/i';
// preg_match_all returns an associative array
preg_match_all($pattern, $content, $matches);
foreach ( $matches[0] as $address) {
$email = filter_var($address,FILTER_VALIDATE_EMAIL);
if ( $email ) {
$email = preg_replace('/\./', '[dot]', $email);
$email = preg_replace('/\@/', '[at]', $email);
$content = str_replace($address,$email,$content);
}
}
return $content;
};
echo sanitize_email('Hello Sir dinesh.shardiwal@gmail.com are you. your email is rakesh.shardiwal@gmail.com');
Output
Hello Sir dinesh[dot]shardiwal[at]gmail[dot]com are you. your email is rakesh[dot]shardiwal[at]gmail[dot]com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment