Email Address Encoder – CodeIgniter Helper, Stop Email Address harvesting!

Spiders/Email Address Hunters are hungry to get your email address from website. You can buy such software/ web scrapper that collects email address for Internet Marketing, Spamming etc too if you wish ;). But you want to avoid those crawlers to collect your email addresses from your business directory or site if you encode the email address or make an image with the email address. I’ve built a Codeigniter Helper to encode your email address easily. You can use those functions on other frameworks, no copyright – just copy paste it :p

Here is the helper file

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

/**
 * CodeIgniter encode_email Helpers
 * * 
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Musa
 * @link		https://kodegeek.wordpress.com/2010/04/17/encode_email_codeigniter_helper/
 */

// ------------------------------------------------------------------------


	/*
	 * Find out all email address and encode accordingly
	 * 
	 * @param string $content
	 * 
	 * @return string
	 */
	function find_encode_email($content=null){
		if(is_null($content))	return false;
		
		//Regular Expression For Email Address
		$regex = "/[a-z0-9.-_]{3,}[@][a-z]{1,}[.]co(m|[.][a-z]{2})/";
		$content = preg_replace_callback($regex, 'my_callback', $content);
		
		return $content;
	}
	
	/*
	 * Call back function use to replace pattern
	 * 
	 * @param array $matches
	 * 
	 * @return string
	 */
	function my_callback($matches) {
		if(isset($matches[0]))
			return encode_email($matches[0]);
		else
			return $matches[1];
	}

	
	/*
	 * Encode email address
	 * 
	 * @param string $email
	 * @param string $title
	 * @param boolean $anchor
	 * @param string $encode_type
	 * 
	 * @return string $buffer	- encoded email address	
	 */
	function encode_email($email, $title='', $anchor=false, $encode_type=null){
						
						if ($title == "")
            				$title = $email;
						
						//if javascript off
						if($encode_type=='noscript')
						$email = str_replace(array('@', '.'), array(' at ', ' dot '), $email);

						$bit = array();
					    if ($anchor == TRUE)
        				{ 
            				$bit[] = '<'; $bit[] = 'a '; $bit[] = 'h'; $bit[] = 'r'; $bit[] = 'e'; $bit[] = 'f'; $bit[] = '='; $bit[] = '\"'; $bit[] = 'm'; $bit[] = 'a'; $bit[] = 'i'; $bit[] = 'l';  $bit[] = 't'; $bit[] = 'o'; $bit[] = ':';
        				}
				        for ($i = 0; $i < strlen($email); $i++)
				        {
            				$bit[] .= " ".ord(substr($email, $i, 1));
				        }

					        $temp	= array();
        
					        if ($anchor == TRUE)
					        {        
					            $bit[] = '\"'; $bit[] = '>';
					            
					            for ($i = 0; $i < strlen($title); $i++)
					            {
					            	$ordinal = ord($title[$i]);
								
									if ($ordinal < 128)
									{
										$bit[] = " ".$ordinal;            
									}
									else
									{
										if (count($temp) == 0)
										{
											$count = ($ordinal < 224) ? 2 : 3;
										}
									
										$temp[] = $ordinal;
									
										if (count($temp) == $count)
										{
											$number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
							
											$bit[] = " ".$number;
											$count = 1;
											$temp = array();
										}   
									}
					            }
					            
					            $bit[] = '<'; $bit[] = '/'; $bit[] = 'a'; $bit[] = '>';
					       }
       				        
				        $bit = array_reverse($bit);
				        ob_start();
				        
				        ?>
<script type="text/javascript">
//<![CDATA[
var l=new Array();
<?php
    
    $i = 0;
    foreach ($bit as $val)
    {
?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php
    }
?>

for (var i = l.length-1; i >= 0; i=i-1){ 
if (l[i].substring(0, 1) == ' ') document.write("&#"+unescape(l[i].substring(1))+";"); 
else document.write(unescape(l[i]));
}
//]]>
</script>				<?php

        $buffer = ob_get_contents();
        ob_end_clean(); 
        
		return $buffer;				
	}

/* End of file encode_email_helper.php */
?>

Use it like other Codeigniter helper, Here is example to call this function from view

  echo find_encode_email(PAGE_CONTENT_HERE); //this will find an email address on text and replace with encoded value
  echo encode_email(YOUR_EMAIL_ADDRESS_HERE); //this will encode email address
 echo encode_email(YOUR_EMAIL_ADDRESS_HERE, TITLE, true );//this will generate an anchor with encoded value

Thanks

Advertisements

3 thoughts on “Email Address Encoder – CodeIgniter Helper, Stop Email Address harvesting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s