如何让文本适应设定的表格宽度而不是拉伸表格?

发布于 2024-09-14 21:27:00 字数 464 浏览 3 评论 0原文

我认为这会制作一个具有设定宽度的表格,并且文本会自动尝试通过新行开始来适应。然而,表格仍然被长行文本拉长。

<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

我想我在这里忘记了一些绝对重要的东西.. 0.o

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灰色世界里的红玫瑰 2024-09-21 21:27:01

文本需要有空格才能适合该宽度。如果您有一个非常长的单词,它将完全显示在一行上。您可以使用 php 函数 wordwrap,它允许您将行的宽度设置为一定数量的字符(http:// /php.net/wordwrap)

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)

今天小雨转甜 2024-09-21 21:27:01

尝试通过 CSS 做到这一点:

word-wrap:break-word 

同时将 width= 更改为 style="width: 300px"

Try do it by CSS:

word-wrap:break-word 

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

心的憧憬 2024-09-21 21:27:00

将代码更改为:

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

PHP 中有一个内置函数,名为 wordwrap对于此类任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文