Created
April 17, 2012 23:47
-
-
Save webaware/2409958 to your computer and use it in GitHub Desktop.
base64 encode email addresses in PHP, and replace with decoded addresses client-side in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* I don't like this sort of thing, but I have a client who needs it. | |
* What I did for them was to put the email address on the page encoded in base64, | |
* and use client-side script to decode it. | |
*/ | |
// server-side... | |
/** | |
* return HTML for a base64-encoded email link | |
* @param string $emailAddress the email address to encode | |
* @param string $text HTML-clean text to show for the link | |
* @param boolean $keepText TRUE to keep the text shown, FALSE (default) to replace with email address | |
* @return string | |
*/ | |
function getEncodedEmail($emailAddress, $text, $keepText = FALSE) { | |
$encoded = base64_encode($emailAddress); | |
$classes = $keepText ? 'encoded-hidden keep-text' : 'encoded-hidden'; | |
return "<span class='$classes' data-enc-email=\"$encoded\">$text</span>"; | |
} | |
// client-side... | |
<script> | |
/** | |
* find spans with encoded email data, and replace contents with email links | |
*/ | |
function decodeEmails() { | |
var elements, element, i, len, a, text, email, dataName = "data-enc-email"; | |
function getSelected() { | |
// use select query if available | |
if (document.querySelectorAll) | |
return document.querySelectorAll("span[" + dataName + "]"); | |
// no select query so go old school | |
var found = []; | |
elements = document.getElementsByTagName("SPAN"); | |
// assume IE, so use IE-best loop style | |
for (i = elements.length; i--; ) { | |
if (simplebase.hasAttribute(elements[i], dataName)) { | |
found.push(elements[i]); | |
} | |
} | |
return found; | |
} | |
elements = getSelected(); | |
for (i = 0, len = elements.length; i < len; ++i) { | |
element = elements[i]; | |
email = simplebase.decodeBase64_UTF8(element.getAttribute(dataName)); | |
if (element.className.indexOf("keep-text") != -1) | |
text = element.textContent || element.innerText; | |
else | |
text = email; | |
a = document.createElement("A"); | |
a.href = "mailto:" + email; | |
a.appendChild(document.createTextNode(text)); | |
element.innerHTML = ""; | |
element.appendChild(a); | |
element.className = element.className.replace("encoded-hidden", ""); | |
} | |
} | |
/** | |
* decode a base64 encoded UTF-8 string | |
* ref http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html | |
* decodeBase64 here: http://phpjs.org/functions/base64_decode:357 | |
* @param {String} data the base64 encoded UTF-8 string | |
* @return {String} | |
*/ | |
decodeBase64_UTF8: function(data) { | |
return decodeURIComponent(escape(decodeBase64(data))); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment