BoeNoe
BoeNoe

Reputation: 562

Line Won't Break In PHP

In the code below I have tried to use the wordwrap function to make a new line every 255 characters. This is to prevent a long text from going off the screen. Could any help me?

 <?php 
$get  = new mysqli('', '', '', '');
$sql = "SELECT * FROM messages ORDER BY id ASC";
$result = $get->query($sql);

    while($r = $result->fetch_assoc()) {
        echo "<div class='chat'><p class='u'>" . $r['username'] . "</p><br /><br /><p class='m'>" . wordwrap($r['message'], 255, '<br />\n') . "</p></div><hr /><br /><br /><br />";
    }
?> 

Upvotes: 0

Views: 49

Answers (3)

Mark H.
Mark H.

Reputation: 548

Set the fourth parameter of wordwrap() to true in order force a break for words longer than the specified number of characters:

wordwrap($r['message'], 255, "<br />\n", true);

Upvotes: 5

Ryan Churchill
Ryan Churchill

Reputation: 81

try

  $str =$r['message'];
  echo wordwrap($str,15,"<br>\n");

The width is not done in characters

Upvotes: -1

Duane Lortie
Duane Lortie

Reputation: 1260

For the \n to be converted correctly it needed to be in double quotes

wordwrap($r['message'], 20, "<br />\n");

Upvotes: 0

Related Questions