user2510039
user2510039

Reputation: 167

break long text/string to a new line

I am trying to fetch data from mysql database and I am experiencing some problem. 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. 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.

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);
    ?> 

Upvotes: 1

Views: 2787

Answers (1)

nice ass
nice ass

Reputation: 16729

See wordwrap. Something like:

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

But it might be a better idea to do this with CSS (word-wrap: break-word)

Upvotes: 1

Related Questions