Reputation: 1411
I'm currently working on a PHP script to check and correct wrong typed email addresses. If the input is: htomail.com, the script will change it to hotmail.com
I'm using the following code (for all possible typos):
$input = '[email protected]';
$pattern = '/htomail.com/';
$replacement = 'hotmail.com';
$output = preg_replace($pattern, $replacement, $input);
echo $output;
etc.
But I'm wondering if it's possible to use a (sort of) spellchecker / autocorrect function to correct all possible typos.
Upvotes: 0
Views: 1063
Reputation: 48
$wrong_email = "[email protected]";
ereg_replace(Array[], "@hotmail", $wrong_email);
The ereg_replace function will be very helpful if you could put all the frequent wrongs in an Array, so the function can look up for the wrongs using the array.
Upvotes: 1
Reputation: 8583
You would be better off telling the user that their email address appears invalid and allowing them to fix the mistake.
I use this code to make sure that the email is in a valid format and the domain name has a valid MX record.
function validEmail($string){
// correct format
if (filter_var($string, FILTER_VALIDATE_EMAIL)){
// valid domain name
list($userName, $mailDomain) = explode("@", $string);
if (checkdnsrr($mailDomain, "MX")) {
return true;
} else {
return false;
}
}else{
return false;
}
}
Upvotes: 3
Reputation: 5359
Php have pspel bundled extension for that: http://php.net/manual/en/ref.pspell.php But I think you should for check domain use whois service.
Upvotes: 0
Reputation: 179
You will be dealing with heuristics. You will be better of looking for a library that already handles it for you. You pretty much have to check an entire database to see if the word exists, check if it is spelled correctly, see if any other words would match the syntax, and let the user know which word is the correct one. Like in MS WORD spell check
Upvotes: 1