简体   繁体   English

将textarea中的html标记转换为富文本

[英]Convert html tag in textarea to rich text

I am using PHP to fill a textarea: 我正在使用PHP填充文本区域:

<textarea text-input" id="Desc" rows="15" wrap="soft" name="Desc"><?php echo $Desc; ?></textarea> 

but the textarea is showing HTML tags in it, which I don't want. 但是textarea中显示HTML标记,这是我不想要的。

<b>Analog And Digital System</b><br />
<b>Analog System:</b><br />
A string tied to a doorknob would be an analog system. They have a value that changes steadily over time and can have any one of an infinite set of values in a range. If you put a measuring stick or ruler at a specific point along the string, you can measure the string's value every so many seconds at that point. When you watch it move, you will see it moves constantly. It doesn't instantly jump up and down the ruler. <br />
<br />
<b>Digital System:</b><br />
A digital system would be to flick the light switch on and off. There's no 'in between' values, unlike our string. If the switch you are using is not a dimmer switch, then the light is either on, or off. In this case, the transmitter is the light bulb, the media is the air, and the receiver is your eye. This would be a digital system.<br />

Basically what I want to do is convert <b> tags to make the content bold and <br> tags to new lines. 基本上,我想做的是将<b>标记转换为粗体,将<br>标记转换为新行。

I want to do it without using any editor. 我想不使用任何编辑器就做。

If you want to show only the HTML output, then use a div tag instead of textarea . 如果您只想显示HTML输出,请使用div标签而不是textarea Because, we cannot show Rendered HTML inside a textarea . 因为,我们无法在textarea显示Rendered HTML。

If you want to show HTML output and want that editable, use contenteditable attribute on div tag. 如果要显示HTML输出并希望其可编辑,请在div标签上使用contenteditable属性。

More about contenteditable and it's support can be found at 有关contenteditable及其支持的更多信息,请访问:

https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable

There is no way to to do HTML formatting inside textarea you will have to use ant editor, but if you want to show it in div then setting the attribute contentEditable="true" will do the trick 无法在textarea内进行HTML格式化,您将不得不使用ant编辑器,但是如果您想在div中显示它,则设置属性contentEditable =“ true”将可以解决问题。

more references here (already asked on stackoverflow) 这里有更多参考(已经对stackoverflow提出了要求)

You should use javascript based WYSIWYG HTML Editors like tinymce and CKEditor for this purpose. 为此,您应该使用基于JavaScript的WYSIWYG HTML编辑器,例如tinymceCKEditor

Otherwise the HTML code will remain as they are (plain text). 否则,HTML代码将保持原样(纯文本)。

But if you use the editors, the user will be able to edit the content and the content will be converted to rich text using javascript (and it's exactly what the editors will do for you magically). 但是,如果您使用编辑器,则用户将能够编辑内容,并且内容将使用javascript转换为富文本格式(这正是编辑器将为您神奇地完成的工作)。

WYSIWYG = What You See Is What You Get 所见即所得 = 所见即所得

You should use the code in here Converting <br /> into a new line for use in a text area 您应该使用此处的代码将<br />转换为新行以在文本区域中使用

<?  
   $text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";
   $breaks = array("<br />","<br>","<br/>");  
   $text = str_ireplace($breaks, "\r\n", $text);  
?>  
<textarea><? echo $text; ?></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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