function Markdown_Parser::encodeEmailAddress in Markdown 6
Same name and namespace in other branches
- 5 markdown.php \Markdown_Parser::encodeEmailAddress()
1 call to Markdown_Parser::encodeEmailAddress()
File
- ./
markdown.php, line 1497
Class
Code
function encodeEmailAddress($addr) {
#
# Input: an email address, e.g. "foo@example.com"
#
# Output: the email address as a mailto link, with each character
# of the address encoded as either a decimal or hex entity, in
# the hopes of foiling most address harvesting spam bots. E.g.:
#
# <p><a href="mailto:foo
# @example.co
# m">foo@exampl
# e.com</a></p>
#
# Based by a filter by Matthew Wickline, posted to BBEdit-Talk.
# With some optimizations by Milian Wolff.
#
$addr = "mailto:" . $addr;
$chars = preg_split('/(?<!^)(?!$)/', $addr);
$seed = (int) abs(crc32($addr) / strlen($addr));
# Deterministic seed.
foreach ($chars as $key => $char) {
$ord = ord($char);
# Ignore non-ascii chars.
if ($ord < 128) {
$r = $seed * (1 + $key) % 100;
# Pseudo-random function.
# roughly 10% raw, 45% hex, 45% dec
# '@' *must* be encoded. I insist.
if ($r > 90 && $char != '@') {
/* do nothing */
}
else {
if ($r < 45) {
$chars[$key] = '&#x' . dechex($ord) . ';';
}
else {
$chars[$key] = '&#' . $ord . ';';
}
}
}
}
$addr = implode('', $chars);
$text = implode('', array_slice($chars, 7));
# text without `mailto:`
$addr = "<a href=\"{$addr}\">{$text}</a>";
return $addr;
}