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]
  • 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:[email protected]\">[email protected]</a> is our primary <a href=\"mailto:[email protected]\">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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.