HTML Entitize any string





12
Date Submitted Wed. Aug. 30th, 2006 5:06 AM
Revision 1 of 1
Helper dohpaz
Tags entities | HTML | PHP
Comments 2 comments
This function is useful in helping to deter spam bots by obfuscating things such as e-mail addresses and URLs that are displayed on a web page. While it's not 100% fool proof, it does offer some protection.

Example:
$email = html_entitize('foo@baz.org');
/*
Outputs foo@baz.com
*/

function html_entitize($string) {
    $encoded = null;

    for ($i = 0, $j = strlen($string); $i < $j; $i += 1) {
        switch (rand(0,1)) {
            case 0:
                $encoded .= sprintf('&#%s;', ord($string[$i]));
                break;
            case 1:
                $encoded .= sprintf('&#x%s;', dechex(ord($string[$i])));
                break;
        }
    }

    return $encoded;
}
 

Ken S

Comments

Comments Smarter spambots
Mon. Sep. 4th, 2006 12:09 PM    Scripter sehrgut
  Comments You can use an image
Thu. Sep. 7th, 2006 10:07 PM    Helper jbplou

Voting