0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Chris
  • 2,905
  • 5
  • 29
  • 30
  • `
    `? Seriously? Is it 1995 again? Anyway: What you want to happen? The text to wrap, the text to be cut off, or something else?
    – RoToRa Aug 20 '10 at 13:34
  • I have already found the solution using wordwrap as you can see below, what's wrong with
    for centering an HTML table?
    – Chris Aug 20 '10 at 14:36

3 Answers3

1

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.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • Thank you, I left out width="300" and use the 2nd parameter of wordwrap now to set the length. – Chris Aug 20 '10 at 13:39
1

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)

A. M.
  • 580
  • 1
  • 8
  • 21
0

Try do it by CSS:

word-wrap:break-word 

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

Jim
  • 22,354
  • 6
  • 52
  • 80
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82