| > Projects > PHP > Strip Phone Numbers and Email Addresses |
PHP > Strip Phone Numbers and Email Addresses
I used this on Sitter Seeker to remove contact details from babysitters profile. This was to make sure the clients privacy was kept, hide emails from spammers and to keep the community safe.
With PHP's regular expression features we can replace anything that resembles a email address or phone number.
stripContacts can find and remove virtually anything that could give away someones phone number or email. Here is a small list of numbers and email addresses that would be filtered.
- 206 577 9999
- 1-800-884-9382
- 884 933-0993
- 3829993382
- 666.223.5678
- sd@gh.de
- billnine@superman.com
<?php
/**
* STRIPCONTACTS
* Replaces postal codes and email addresses with place holders
*
* @param String $str Message with contact details you wish to have filtered
* @return String The message without contact details
*/
function stripContacts($str)
{
// remove email addresses
$str = eregi_replace("[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})", "my email address", $str);
// remove phone numbers
$str = ereg_replace("([0-9](-| |.))?(\(?[0-9]{3}\)?(-| |.)?)?[0-9]{3}(-| |.)?[0-9]{4}", "my phone number", $str);
return $str;
}
// echo stripContacts('Email me at green@eggs.ham');
?>
|