将长文本/字符串拆分为新行

时间:2013-06-21 20:44:15

标签: php

我正在尝试从mysql数据库中获取数据,我遇到了一些问题。我有一个表单,用户可以输入文本,我可以将其发送到数据库,但在显示它时,如果文本太长,那么文本不适合表格。我希望在 20个字符之后有一个新行。怎么可能?即使我将表格宽度设置为某些像素,文本也会脱离表格。

这一行有一个很长的文字,我想每20个字符换一行。

echo "<td>" . $row['lastname'] . "</td>"; 



<?php
       $con=mysqli_connect("localhost","root","","users");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con,"SELECT * FROM users");

    echo "<table border='1' width='640px'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['firstname'] . "</td>";
      echo "<td>" . $row['lastname'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    mysqli_close($con);
    ?> 

1 个答案:

答案 0 :(得分:1)

wordwrap。类似的东西:

echo wordwrap($yourString, 20, '<br>');

do this with CSSword-wrap: break-word

可能更好