Category: Web development

  • Disguise Email Addresses for online publishing

    Disguise your email address or any text with this character obfuscation. This code corey@example.com will show up on a web page as [email protected]. You can share your email address without worrying that it will be collected by a spam bot.

    Enter some plain text



    Some losers send spam email for a living, and will send garbage to any email they can find online. Obfuscating email addresses in character codes cloaks them from some of the leeches. I decided to create code to automate this task.

    Here is an ASP classic function that will convert a string to ASCII characters. PHP code below. These characters will display as normal text to the casual user. The difference between alphabet characters and ASCII characters is that encoded characters must be evaluated before they look like an email address. This thin veil of secrecy is enough to fight off some email harvesters.

    
    public function asciiDisguise( string )
    	build = ""
    	for i=1 to len( "" & string )
    		build = build & "&#" & asc( mid( string, i, 1 )) & ";"
    	next
    	asciiDisguise = build
    end function

    Here is the same function in PHP.

    
    function asciiDisguise( $str ){
    	$build = "";
    	for( $i=0;$i<strlen( $str );$i++ ){
    		$build .= "&#" . ord( substr( $str, $i, 1 )) . ";";
    	}
    	return $build;
    }
    
  • PHP4 Friendly htmlspecialchars_decode

    I needed to use the PHP function htmlspecialchars_decode( ) for a WordPress widget I am making. This function is built into PHP versions 5.1.0 and greater and is used to convert special HTML entities to characters. As defined, htmlspecialchars_decode( ) is the opposite of htmlspecialchars( ). Someone named Thomas commented on the PHP man page to point out a flaw in the definition. He also provides some code, which I have only modified slightly below to check function_exists( ).

    
    if ( !function_exists('htmlspecialchars_decode') ){
        function htmlspecialchars_decode($string,$style=ENT_COMPAT)
        {
            $translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS,$style));
            if($style === ENT_QUOTES){ $translation['&#039;'] = '\''; }
            return strtr($string,$translation);
        }
    }