Grow your CSS skills. Land your dream job.

Last updated on:

Email Protector

<?php
	function php_split_js_make_email($phpemail)
	{
		$pieces = explode("@", $phpemail);
	
		echo '
			<script type="text/javascript">
				var a = "<a href=\'mailto:";
				var b = "' . $pieces[0] . '";
				var c = "' . $pieces[1] .'";
				var d = "\' class=\'email\'>";
				var e = "</a>";
				document.write(a+b+"@"+c+d+b+"@"+c+e);
			</script>
			<noscript>Please enable JavaScript to view emails</noscript>
		';
	}
?>

Usage

<?php php_split_js_make_email("youremail@here.com"); ?>

Reference URL

Comments

  1. Permalink to comment#

    That’s a nice way to obscure an email. I’ve always used WordPress’ antispambot&sup1;. But for a project outside of WP…

    &sup1;http://codex.wordpress.org/Function_Reference/antispambot

  2. Chester
    Permalink to comment#

    Where would I put the PHP function? Inline with the html?
    Will a PHP function run in my index.html?

    Thanks.

  3. Permalink to comment#

    @chester – you can run php in .html files, but you might also need to enable apache via .htaccess to run php in a .html with a line like this in your .htaccess file:

    AddType application/x-httpd-php .html .htm

  4. sandy
    Permalink to comment#

    i want to a php code to detach the mail from body .. please give reply..

  5. I actually updated this email protector and it
    can be found here with a couple of new arguments (Same link as above)

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".