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

jjjwad0x  于 2023-04-19  发布在  PHP
关注(0)|答案(5)|浏览(205)

我使用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>标签到新的行。
我不想使用任何编辑器。

k10s72fa

k10s72fa1#

如果你只想显示HTML输出,那么使用div标签而不是textarea。因为,我们不能在textarea中显示渲染的HTML。
如果你想显示HTML输出并希望它是可编辑的,在div标签上使用contenteditable属性。
有关contenteditable及其支持的更多信息,请访问
https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable

1qczuiv0

1qczuiv02#

在textarea中没有办法进行HTML格式化,你必须使用ant编辑器,但是如果你想在div中显示它,那么设置属性contentEditable=“true”就可以了
更多引用here(已在stackoverflow上询问)

ctrmrzij

ctrmrzij3#

您应该使用基于javascript的所见即所得HTML编辑器,如tinymce和**CKEditor**。
否则,HTML代码将保持原样(纯文本)。
但是如果你使用编辑器,用户将能够编辑内容,内容将被转换为使用javascript的富文本(这正是编辑器将为你神奇的)。

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

uxh89sit

uxh89sit4#

你应该使用这里的代码Converting into a new line for use in a text area

<?  
   $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>
ubof19bj

ubof19bj5#

正如其他人所解释的那样。我有一些解决方案,如下面的代码。实际上,我创建了一个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>

相关问题