将文本区域中的html标记转换为富格文本

Convert html tag in textarea to rich text

本文关键字:文本 转换 html 区域      更新时间:2023-09-26

我正在使用PHP填充一个文本区域:

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

基本上,我想做的是将<b>标记转换为粗体内容,将<br>标记转换为新行。

我想在不使用任何编辑器的情况下完成它。

如果只想显示HTML输出,请使用div标记而不是textarea。因为,我们无法在textarea中显示"渲染HTML"。

如果要显示HTML输出并希望其可编辑,请使用div标记上的contenteditable属性。

有关contenteditable及其支持的更多信息,请访问

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

没有办法在文本区域内进行HTML格式化,您必须使用ant编辑器,但如果您想在div中显示它,那么设置属性contentEditable="true"将实现

此处有更多参考资料(已在stackoverflow上询问)

为此,您应该使用基于javascript的WYSIWYGHTML编辑器,如tinymceCKEditor

否则,HTML代码将保持原样(纯文本)。

但是,如果您使用编辑器,用户将能够编辑内容,并且内容将使用javascript转换为富文本(这正是编辑器神奇地为您所做的)。

WYSIWYG=所见即所得

您应该使用此处的代码转换<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>

正如其他人所解释的。我有一些解决方案,如下面的代码。实际上,我创建了一个div,然后将其转换为textarea然后是我的php代码。

它对我很有效,请马上试试。我想你已经得到了答案。

<div style="height: 100px;overflow-y: scroll;text-rendering:auto;overflow: auto;resize: vertical;">
    
    <?php
    //this is my chat.
       $text = " Rohit | 14-04-2023 12:32:31 
                 <b>nilesh bhai </b> 
     
                Rohit  | 13-04-2023 19:17:17 
                <b>this is correct</b> ";
     
         echo nl2br($text); 
        // answer is
         //  M3 Insurance | 14-04-2023 12:32:31
         //  **nilesh bhai**
    
        //   M3 Insurance | 13-04-2023 19:17:17
         //  **this is correct**
    ?>
    
  </div>