简体   繁体   English

将长文本/字符串换行

[英]break long text/string to a new line

I am trying to fetch data from mysql database and I am experiencing some problem. 我试图从mysql数据库中获取数据,但遇到了一些问题。 I have a form where the user be able to enter text and I am able to send it to the database however while displaying it back, if the text is too long then the text doesn't fit in the table. 我有一个表单,用户可以输入文本,并且可以将其发送到数据库,但是在显示文本时,如果文本太长,则文本不适合表格。 I would like to have a new line after 20 characters . 我想在20个字符后换行。 How is that possible? 那怎么可能? Even though I set the table width to certain pixels, the text goes out of the table. 即使我将表格宽度设置为某些像素,文本也会从表格中消失。

this row has a long text and I want to have a new line every 20 characters. 该行的文字很长,我想每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);
    ?> 

See wordwrap . 请参阅自动wordwrap Something like: 就像是:

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

But it might be a better idea to do this with CSS ( word-wrap: break-word ) 但这可能是使用CSS更好的主意( word-wrap: break-word

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM