3214 Posts in 848 Topics by 810 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 213 Views |
-
Encode email addresses in content text

9 February 2013 at 2:41am
Hi,
How should I encode email addresses in content text in my templates so that spam bots won't recognize them? I've tried a couple of modules, but they either throw errors or do nothing and all and none of them have any kind of documentation about how to use them.
-
Re: Encode email addresses in content text

9 February 2013 at 3:14pm
So you have email addresses embedded within a text field? Email::obfuscate() handles encoding email addresses to 'hide' the addresses but this only works on a single email (it won't replace all email addresses within content).
I'd do something like.. (not tested, but should give you a start)
function getEmailEscapedContent() {
$content = $this->Content;
$callback = function($matches) {
Email::obfuscate($matches[0]);
};return DBField::create_field('HTMLText', preg_replace_callback("/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i", $callback,$content));
}Then instead of using $Content to output, use $EmailEscapedContent
-
Re: Encode email addresses in content text

12 February 2013 at 12:11am Last edited: 12 February 2013 12:13am
Thanks, that works (after I added 'return' to the callback, in case some else ever tries this
.
edit:
I ended up putting this code in my PageController, so I can escape any field, not just content
function EscapeEmail($content) {
$callback = function($matches) {
return Email::obfuscate( $matches[0], 'hex' );
};return DBField::create_field( 'HTMLText', preg_replace_callback( "/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i", $callback, $content ) );
}And in template:
$EscapeEmail($Content)
| 213 Views | ||
|
Page:
1
|
Go to Top |


