PHP
--
Questions
--
Followers
Top Experts
Hi,
I have a mySQL longtext field that I want to display on a web page using PHP. The data in the field is formatted. I want to preserve the formatting so I'm using a textarea for the data. The problem is I am unable get the textarea to autoresize to fit the content. I tried <textarea autoresize> but that does not work. If I assign the height of the textarea to the length of the content I get a large whitespace.
What is the best way to accomplish this?
This is what I have currently:
echo '<textarea style="border: none; width: 100%; min-height: ' . strlen($company_info) . 'px; height: auto;">' . $company_info . '</textarea>';
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
If you don't need to post the data as a form field, then I think that is making it more complicated than you need.
What do you mean by the data is formated?
Perhaps you really mean to use the pre tag? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
By formatted I mean the data contains HTML for line breaks, bolding, etc. If I use the <pre> tag it changes the font and does not look good overall.
Ok, the PRE tag will maintain hidden line breaks.
If that is the case, why not just put the data in a div tag? Or is this meant to go into a WYSIWYG type of box to be submitted by a form?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
If you are going to allow somebody to edit the text that will later be submitted by a form, use either https://ckeditor.com/ or https://www.tiny.cloud/ Those are both WYSIWYG editors that will have the functionality to preserve formating and allow editing with formating.
As an aside, it may not be a good idea to allow some of that HTML to be saved in it's raw format because it could open you up for an SQL injection attack.
The data is for display only. There will no submission of the data. The data comes from another source.
Then you should use the div tag to display your data.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The DIV tag does not preserve the formatting. How do I keep the line breaks, etc.?
It sure does. If you have HTML line breaks using <br> tags or <p> tags the line breaks will work. If you have line carriage returns, then you need to use the pre tag.
Give us some sample data to help you better. As example, you can see how it works below.
https://jsbin.com/?html,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<pre>
<h1>title</h1>
Text in a pre element
is displayed in a fixed-width
font, and it preserves
both <b> spaces and</b>
line <br>breaks
</pre>
<div>
<h1>title</h1>
Text in a pre element
is displayed in a fixed-width
font, and it preserves
both <b> spaces and</b>
line <br>breaks
</div>
</body>
</html>





EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
If the line breaks are simple carriage return/line feeds, the only html tag I'm aware of that recognizes that is the PRE tag.
Simple HTML has no concept of them.
For example:
<html>
I
am
text
with
cr/lf
in it
</html>
Renders as:
I am text with cr/lf in it
Alright here is my proof that a div tag does not preserve any formatting.
https://techwithheartnetwork.dev/members-profile/?ID=288
Go to the Company Info section. The first block of text is a textarea. The second is a div. The third is a div with pre. Obviously my prference is the textarea, however I do not know how to autosize the height.
Correct, a DIV tag won't honor those.
I suppose you could replace the CR/LF's with <br /> in PHP and then a div should work.
Never really used the textarea control myself but Google seems to say you'll need Javascript to get it to auto resize.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Sounds good. How would I identify the cr/lf in PHP?
It's important to remember that this function does NOT replace newlines with <br> tags. Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! This caused problems for me regarding a function I was writing -- I forgot the newlines were still being preserved.
If you don't want newlines, do:
<?php
$Result = str_replace( "\n", '<br />', $Text );
?>
I know this already has an accepted solution but I feel like this probably should have been solved with a div tag that used the white-space CSS rule.
Using nl2br only adds the <br> tag to line break white space but it doesn't do anything for other whitespace like indents (e.g. tabs).
The div tag should be the proper container and the white-space CSS attribute can tell the browser how to handle white space within the content itself without modifying the content.
Anyway, just throwing that out there. Moving on...






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
PHP
--
Questions
--
Followers
Top Experts
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.