hmaxx
hmaxx

Reputation: 657

wordwrap php not working

I am trying to break a line after 20 characters which I am displaying from mysql database but it is not breaking anything.

what I am getting

verylooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong

what I need

verylooooooooooooooo

oooooooooooooooooooo

 <?php
 $mydb = new mysqli('localhost', 'root', '', 'test');
 $sql = "SELECT * FROM test  order by id DESC  ";
 $result = $mydb->query($sql);
 if (!$result) {
 echo $mydb->error;
 }
 ?> 

<html>
<body>
<div>
<?php
while( $row = $result->fetch_assoc() ){
echo "<div class='Dtitle'>".wordwrap($row['title'], 20, "<br />\n")."</div>";
}
$mydb->close ();
?>
</div>

Upvotes: 0

Views: 807

Answers (3)

user557846
user557846

Reputation:

by default wordwrap does not split words you need to add true as a parameter to the end in order to split words - see the manual page for more

echo "<div class='Dtitle'>".wordwrap($row['title'], 20, "<br />\n",true)."</div>";

Upvotes: 0

Ids Surrrrr
Ids Surrrrr

Reputation: 49

Try style="word-break: break-all;" or maybe overflow:hidden

Upvotes: 0

user1864610
user1864610

Reputation:

You're missing the cut argument to wordwrap.

Try:

echo "<div class='Dtitle'>".wordwrap($row['title'], 20, "<br />\n", true)."</div>";
                                                                    ^^^^

The PHP reference for wordwrap is here

Upvotes: 1

Related Questions