Location: PHPKode > scripts > hide_mail_link > hide_mail_link/inc_hide_mail_link.php
<?php
/*
 * Filename.....: inc_hide_mail_link.php
 * Features.....: Hides email addresses from crawlers.
 *                Good for guestbooks and linklists.
 * 
 * Erstellt am..: 5. Juli 2002
 *       _  __      _ _
 *  ||| | |/ /     (_) |        Wirtschaftsinformatiker IHK
 * \. ./| ' / _ __  _| |_ ___   www.ingoknito.de
 * - ^ -|  < | '_ \| | __/ _ \  
 * / - \| . \| | | | | || (_) | Peter Klauer
 *  ||| |_|\_\_| |_|_|\__\___/  06131-651236
 * mailto.......: hide@address.com
 *
 * Remarks...: The idea for this class is not from me.
 *             I still can see the results from some function working
 *             like this at phpclasses.org from Manuel Lemos.
 *             I wanted to have my own email hiding class
 *             since i did not find the original class
 *             (if there is one) in phpclasses.org.
 *             The code generated is "tidy-proof".
 *             It needs a javascript-enabled browser to work.
 * Changes:
 * 2002-08-21: Some ideas of Kumar (kumar at chicagomodular dot com)
 *             5 new class variables for css and string configurations.
 *             A new function StrlenLimit() is used to cut too long
 *             strings' tail off.
 * 2002-09-10: Some ideas of Martin Schaedler (Martin dot Schaedler at start dot de)
 *             Choose "at" and "dot" or "&#x40;" and "&#x2e;"
 *             In the second case, the email adress is better readable.
 */

class hide_mail_link
{
	var $CssClass = ''; // set an optional css class for the href value of the <a> tag

	// 2002-08-21:
	// Variables to use when $image is not an image, but is text used for the <a> tag:
	// (note: the default for $image is the hidden version of the email address)
	var $MaxStrlen = 0; // if greater than 0, will be used to limit the length of the <a> text ($image)
	var $StrlenLimitTrail = '...'; // used as a trail when <a> text ($image) is cut by StrlenLimit()
	var $MailtoTextSeparator = '_'; // used to separate elements of the <a> text ($image)
	var $MailtoTextPrefix = ''; // added to the beginning of <a> text ($image)
    
    // 2002-09-10: Choose entity mode 0=normal (=old) 1=entities (=new)
    var $use_entities = 0; // 0: gives "at" and "dot"
                           // 1: gives "@" and "." as html-entities
    var $entities=array(array('@'=>'at','.'=>'dot'),array('@'=>'&#x40;','.'=>'&#x2e;'));
	
    function Put( $mailto='', $subject='', $body='', $image='' )
    # $mailto : the email address (not validated)
    # $subject: optional subject of the mail
    # $body   : optional bodytext of the mail
    # $image  : optional text or image-tag to be shown as link
    {
        $seps   = array( '?','&amp;' );
        $given  = 0;
        
        if( $subject > '' )
        {
            $subject = 
                $seps[ $given ].
                'Subject='.
                rawurlencode($subject);
            $given++;
        }
        
        if( $body > '' )
        {
            $body = 
                $seps[ $given ].
                'Body='.
                rawurlencode($body);
            // $given++; // not necessary
        }
    	
        // str_replace with arrays needs php 4 and is not compatible
        // with php 3
        
        $hidden = str_replace( '@', $this->MailtoTextSeparator.
        $this->entities[$this->use_entities]['@'].
        $this->MailtoTextSeparator, $mailto );
        
        $hidden = str_replace( '.', $this->MailtoTextSeparator.
        $this->entities[$this->use_entities]['.'].        
        $this->MailtoTextSeparator, $hidden );
        
        $minus  = str_replace( $this->MailtoTextSeparator, '-', $hidden );
        
        if( $image == '' ) $image = $hidden;
        // less strict than '^<img.+>$', image may be any <tag>, 
        // not only the <img> tag. <div>, <span> or <object> are ok, too.
        $image_is_tag = (eregi('^<.+>$',$image)) ? TRUE: FALSE;
        
        $split = explode( '@', $mailto );

        echo '<a ';
        if( !empty($this->CssClass) ) echo 'class="'.$this->CssClass.'" ';
        echo 'href="mailto:'.$minus.$subject.$body.'" '.
            'title="mail to '.$minus.'" '.
            'onclick="location.href='.
            "'mail'+'to'+':'+'".
            $split[0]."'+unescape('%40')+'".
            $split[1].
            $subject.$body.
            "';return false;".
            '">';
        if( $image_is_tag == FALSE )
        {
        	$image = $this->MailtoTextPrefix . $image;
        	if(!empty($this->MaxStrlen)) $image = $this->StrlenLimit($image);
        }
        echo $image;
        echo '</a>';
    }
    
    function StrlenLimit($input)
    # @PRIVATE
    # $input : the string to limit, in the case of long email addresses
    {
    	if( strlen($input) > $this->MaxStrlen )
		{
			$output = substr( $input, 0, ($this->MaxStrlen-strlen($this->StrlenLimitTrail)) ).
			$this->StrlenLimitTrail;
		}
		else
		{
			$output = $input;
		}
		return $output;
    }
}    
?>
Return current item: hide_mail_link