Snippet: PHP | JavaScript

PHP and JavaScript email protector / email obfuscator

Now with more arguments!

Here's a new one I wrote! It can be used in exactly the same way, but with a couple more optional arguments. So, now you can do stuff like <?php protect_email("", "Their email");?> - and you should get something like this:

If you wish to store the value in a database rather than echo it to the page, you can use false as the parameter for return.

Code:

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

Edit: Here is the previous article

If you work like me and have databases containing complete emails as strings, but want to make them spam proof to web crawlers then you've probably thought about using, or already use a function like this:

This code will take an email, split it up into little bits, then write you out a little JavaScript code wherever it is called to piece it back together.

<?php
	function protect_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>
		';
	}
?>

Example:

First include the above code in your web page before you plan on using it, then just write <?php protect_email("");?>

Please send me an email if you don't understand to

Code:

Please send me an email if you don't understand to <?php protect_email("")?>