Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Encode email addresses in content text

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 480 Views
  • teel
    Avatar
    Community Member
    5 Posts

    Encode email addresses in content text Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Encode email addresses in content text Link to this post

    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

  • teel
    Avatar
    Community Member
    5 Posts

    Re: Encode email addresses in content text Link to this post

    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)

    480 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.