Log in

View Full Version : PHP cleanEmail Function Error?



lolwut
23-10-2007, 09:43 PM
Before you say cleanEmail(); function doesn't exist, it's a custom one.
What it does is when the user types in their email it changes @'s to [at]'s and .'s to [dot]'s. Anyone who's made use of php.net will know what I'm trying to do. The reason for doing this is to stop SPAM bots scanning the memberlist pages to grab people's emails. You may think about using this yourselves.
Note I'm very new to functions and please forgive me if it's a really stupid error.

Here's my code for the function.


<?php

function cleanEmail($var)
{

$find[0] = "/./";
$find[1] = "/@/";
$replace[0] = " [dot] ";
$replace[1] = " [at] ";
ksort($find);
ksort($replace);

preg_replace($find, $replace, $var);

return $var;

}

echo cleanEmail("[email protected]");
?>
And it just echo's out the unclean email which is [email protected] when it should be echo-ing out someone [at] domain [dot] ext. Any help or fixes appreciated.

lolwut
23-10-2007, 10:18 PM
Sorry for the double but I couldn't edit above.
It does work if you comment out the lines that change a . to a [dot], hope that helps in some way? :S

Invent
23-10-2007, 10:19 PM
EDIT: Here you go:



<?php

function cleanEmail($var)
{

$find = array("/\./", "/@/");
$replace = array(" [dot] ", " [at] ");

$var = preg_replace($find, $replace, $var);

return $var;

}

echo cleanEmail("[email protected]");
?>

lolwut
23-10-2007, 10:26 PM
Thanks alot Simon :]! +Reps. Thread closed as fixed.

Want to hide these adverts? Register an account for free!