Chris
Chris

Reputation: 3025

How to let text adapt to a set table width instead of stretching the table?

I thought that this would make a table with a set width, and that text would automatically try to fit in by starting on a new line. However, the table still gets stretched by long lines of text.

<HTML><center><table width="300" border="1"><tr><td>
<?php
If (file_exists("file.txt")){
    Echo nl2br(file_get_contents("file.txt"));
}Else{
    Echo "File not found.";
}
?>
</td></tr></table></center></HTML>

alt text

I think I'm forgetting something absolutely essential here.. 0.o

Upvotes: 0

Views: 595

Answers (3)

Piotr M&#252;ller
Piotr M&#252;ller

Reputation: 5548

Try do it by CSS:

word-wrap:break-word 

Also change your width= to style="width: 300px"

Upvotes: 0

A. M.
A. M.

Reputation: 565

The text needs to have spaces in order for it to fit in that width. If you have one extremly long word, it will be displayed entirely on one line. You could use the php function wordwrap, which allows you to set the width of the line to a certain number of characters (http://php.net/wordwrap)

Upvotes: 1

shamittomar
shamittomar

Reputation: 46692

Change the code to this:

  echo nl2br(wordwrap(file_get_contents("file.txt")));

There is a built in function in PHP called wordwrap for such tasks.

Upvotes: 1

Related Questions