Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of Tim Burkart
Tim Burkart🇺🇸

How can I auto fit content in a textarea?

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.


Avatar of Scott FellScott Fell🇺🇸

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


Avatar of Tim BurkartTim Burkart🇺🇸

ASKER

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. 


Avatar of Scott FellScott Fell🇺🇸

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?


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Scott FellScott Fell🇺🇸

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. 


Avatar of Tim BurkartTim Burkart🇺🇸

ASKER

The data is for display only. There will no submission of the data. The data comes from another source.


Avatar of Scott FellScott Fell🇺🇸

Then you should use the div tag to display your data. 




Free T-shirt

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.


Avatar of Tim BurkartTim Burkart🇺🇸

ASKER

The DIV tag does not preserve the formatting. How do I keep the line breaks, etc.?


Avatar of Scott FellScott Fell🇺🇸

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>

Open in new window



Avatar of Scott FellScott Fell🇺🇸

I am sorry, that was the wrong link above 


https://jsbin.com/turixocice/edit?html,output


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


What is the 'formatting' you wish to preserve?

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>

Open in new window


Renders as:
I am text with cr/lf in it

Open in new window


Avatar of Tim BurkartTim Burkart🇺🇸

ASKER

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. 


Seems the formatting is as I suggested:  They look like a carriage return/line feed.

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.

Free T-shirt

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.


Avatar of Tim BurkartTim Burkart🇺🇸

ASKER

Sounds good. How would I identify the cr/lf in PHP?


ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)slightwv (䄆 Netminder)

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Pay attention to some of the comments at the bottom just in case you want them actually replaced:
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 );

?>

Avatar of gr8gonzogr8gonzo🇺🇸

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


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

PHP

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.