2

I am using PHP to fill a textarea:

<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.

<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.

I want to do it without using any editor.

SmartDev
  • 481
  • 3
  • 11
  • 30

5 Answers5

4

If you want to show only the HTML output, then use a div tag instead of textarea. Because, we cannot show Rendered HTML inside a textarea.

If you want to show HTML output and want that editable, use contenteditable attribute on div tag.

More about contenteditable and it's support can be found at

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

Sasidhar Vanga
  • 3,384
  • 2
  • 26
  • 47
1

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

more references here (already asked on stackoverflow)

Community
  • 1
  • 1
Vinod Louis
  • 4,812
  • 1
  • 25
  • 46
1

You should use javascript based WYSIWYG HTML Editors like tinymce and CKEditor for this purpose.

Otherwise the HTML code will remain as they are (plain text).

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).

WYSIWYG = What You See Is What You Get

Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79
0

You should use the code in here Converting <br /> 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>
Community
  • 1
  • 1
tcgumus
  • 328
  • 3
  • 8
0

As other have explained. i have some solution like below code. actually I created a div then I converted it as textarea then my php code.

its working for me properly please try it at once. I think you got your answer.

<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>
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – pierpy Apr 16 '23 at 19:57