简体   繁体   中英

Want to get <br> tags from textarea, though no other HTML at all

I'm doing a web builder and I'm having a bit of trouble.

I want to have a textarea that you can enter text that you want to your TEXT element. This element I disabled the HTML when it´s later off previewed by simply putting a .innerText before posting preview. Though, I might just need the <br> tag.

I can use PHP or JS. Any ideas?

Use nl2br(htmlspecialchars($str)) when displaying the text. Note that the order of the function calls matters - first you escape all HTML in the string and then you convert linebreaks to HTML linebreaks.

When you are typing in a textarea, and the return key is pressed,

What actually goes on behind the scenes is this

Hi, \\n There

which produces the following in the textarea.

Hi

There

Hence, what you would need to do is essentially change the \\n (newline) to break tags.

http://php.net/manual/en/function.nl2br.php

只需使用纯文本区域,后缀在HTML页面中显示时就使用nl2br之类的函数

nl2br(htmlspecialchars($string))

将换行符更改为<br />标签,因此您可以将它们显示为html,如在textarea中看到的那样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM