Convert mailto links to images

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Phydeaux
Forum Newbie
Posts: 4
Joined: Fri Sep 05, 2008 10:45 am

Convert mailto links to images

Post by Phydeaux »

I am trying to convert mailto: links to png images for thwarting spambots. I do have a solution working, but it doesn't appear that it would thwart any bot as the email is still there. My users use a custom built CMS system and design their own webpages online. I tell them not to include email addresses, but you know how people listen. Here is some sample code on the display page, after pulling $content from the database:

Code: Select all

$emailreg = "/<a href=\"mailto:([[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+)\">[[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+<\/a>/";
$emailrep = "<img src=\"/includes/texttoimage.php?text=$1\">";
$content = preg_replace($emailreg, $emailrep, $content);
This works great, the texttoimage.php file uses imagettftext and other things to generate the png. However, the source code still has the email address as an argument. A couple of thoughts were to use the texttoimage as a function so it never leaves the code, or to hash/obfuscate the email address, then reverse it before it is written, a la Facebook. The latter might be the easiest, but I'm not sure how to do it and reverse it in the texttoimage. Any thoughts? Thanks.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Convert mailto links to images

Post by andyhoneycutt »

You could base64_encode it, pass that as an argument, then base64_decode it on the other end (in your text_to_image function, before you use the email address).

-Andy
Phydeaux
Forum Newbie
Posts: 4
Joined: Fri Sep 05, 2008 10:45 am

Re: Convert mailto links to images

Post by Phydeaux »

How do I separate it so only the $1 is encoded? Is this going to be in a separate statement, using preg_grep or preg_match first?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Convert mailto links to images

Post by andyhoneycutt »

do whatever processing you would want done to $1 before you pass it to the function. just before you pass it to your function, encode it.
Phydeaux
Forum Newbie
Posts: 4
Joined: Fri Sep 05, 2008 10:45 am

Re: Convert mailto links to images

Post by Phydeaux »

I'm assuming I would use preg_replace_callback, but I'm not finding some useful examples on this. Thank you for your help, you're getting me on the right track, I'm just not getting how to extract the sub expression to use.
Phydeaux
Forum Newbie
Posts: 4
Joined: Fri Sep 05, 2008 10:45 am

Re: Convert mailto links to images

Post by Phydeaux »

Ooo! I figured it out. For those who want to know:

Code: Select all

$emailreg = "/<a href=\"mailto:([[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+)\">[[:alnum:]_\.\-]+\@[[:alnum:]\.\-]+\.+[[:alnum:]\.\-]+<\/a>/";
    function obfuscate_email($string) {
        $emailrep = "<img src=\"/includes/texttoimage.php?text=";
        $myreturn = $emailrep . base64_encode($string[1]) . "\">";
        return $myreturn;
    }
    $content = preg_replace_callback($emailreg, 'obfuscate_email', $content);
Then my texttoimage file base_64decodes it.
Post Reply