Return to Snippet

Revision: 1151
at September 19, 2006 04:38 by mikeejay


Initial Code
/**
 * Description: E-Mail encoder to reduce spam.
 *
 * @author Micke Johansson
 *
 * $mail. The e-mail address to encode.
 * $isLink. Set to true to create a link.
 * $display. What will be displayed in the browser. If omitted it will display the e-mail address.
 *
 * @param string $mail
 * @param bool $isLink
 * @param string $display
 * @return string Encoded e-mail or e-mail link
 */
function EncodeMail($mail, $isLink = false, $display = '')
{
	$domain = substr($mail,strpos($mail, '@')+1);
	$name = substr($mail,0, strpos($mail, '@'));
	$encodedDomain = '';
	$encodedName = '';
	$encodedDisplay = '';
	
	for ($i=0; $i < strlen($domain); $i++)
	{
		$encodedDomain .= '&#'.ord(substr($domain,$i)).';';
	}
	for ($i=0; $i < strlen($name); $i++)
	{
		$encodedName .= '&#'.ord(substr($name,$i)).';';
	}
	for ($i=0; $i < strlen($display); $i++)
	{
		$encodedDisplay .= '&#'.ord(substr($display,$i)).';';
	}
	$script = "<script type=\"text/javascript\">";
	$script .= "d=\"".$encodedDomain."\";";
	$script .= "n=\"".$encodedName."\";";
	if ($isLink)
	{
		if ($display == '')
			$script .= "document.write('<a href=\"&#109;&#097;&#105;&#108;&#116;&#111;&#058;'+n+'&#64;'+d+'\">'+n+'&#64;'+d+'</a>');";
		else
			$script .= "document.write('<a href=\"&#109;&#097;&#105;&#108;&#116;&#111;&#058;'+n+'&#64;'+d+'\">".$encodedDisplay."</a>');";
	}
	else 
	{
		$script .= "document.write(n+'&#64;'+d);";
	}
	$script .= "</script>";
	return $script;
}

Initial URL

                                

Initial Description

                                

Initial Title
E-mail encoder to reduce spam

Initial Tags
php, mail

Initial Language
PHP