Jump to content

obfuscating email addresses using preg_replace


michaellunsford

Recommended Posts

I created a function that turns an email address to an html entity using [color=blue]ord()[/color]. Instead of [color=blue]ord()[/color]ing each letter individualy, I'm wondering if there would be a way to regexp the second parameter of preg_replace?

[code=php:0]preg_replace("/[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/",$what_goes_here,$subject);[/code]
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...
Okay
[list]
[*]the output is correct.
[*]I'm no longer stepping through the entire string, one character at a time.
[*]The code went from 34 lines down to 8 lines.
[/list]
But can it be reduced further?

[code]<?php
$var="<a href=\"mailto:test@example.com\">test@example.com</a> is our primary <a href=\"mailto:contact@example.com\">contact</a>";
$newvar=preg_replace_callback("/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i",create_function('$matches','return obfuscate_me($matches[0]);'),$var);
echo $newvar;
function obfuscate_me($my_array) {
return preg_replace_callback("/[A-Z0-9]/i",create_function('$matches','return "&#".ord($matches[0]).";";'),$my_array);
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.