1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Retain text formatting, without using rich text editor for input?

Discussion in 'XenForo Development Discussions' started by TheBigK, Sep 4, 2015.

  1. TheBigK

    TheBigK Well-Known Member

    My 'Contact Us' addon uses a 'textarea' input field where the user can type his/her message. The addon's working fine, but I'd like the email to retain the formatting of the text entered by the user. I think one of the options is to use the rich text editor; but wondering if I can have the basic formatting (line breaks, for example) retained?

    I use HTML format for all my outgoing emails.
     
  2. Jake B.

    Jake B. Well-Known Member

    You can keep the line breaks by using nl2br. Spaces/tabs you can convert to non-breaking spaces using str_replace. That should cover just about everything :)
     
  3. TheBigK

    TheBigK Well-Known Member

    Hi Jake,

    I changed my code to this:
    PHP:
    $senderMessage nl2br($this->_input->filterSingle('sender_message'XenForo_Input::STRING));
    Email Received:-

    This is line 1<br /> <br /> This is line 2<br /> <br /> This is line 3

    The email is in HTML format, but it's not converting <br/> tags to actual new lines in the body of email.

    Screen Shot 2015-09-05 at 9.57.29 am.png
     
  4. Jake B.

    Jake B. Well-Known Member

    You'll need to use {xen:raw to render it in the template. However, since you are allowing user input you should probably sanitize it as well first.
     
  5. TheBigK

    TheBigK Well-Known Member

    Thanks! I added {xen:raw $message} to the email template and it now retains breaks :D !
     
  6. Jake B.

    Jake B. Well-Known Member

    You will want to sanitize the input as well
     
  7. TheBigK

    TheBigK Well-Known Member

    Umm, how do I do that? o_O
     
  8. Jake B.

    Jake B. Well-Known Member

    Strip out html from what the user inputs before running it through nl2br
     

Share This Page