user569322
user569322

Reputation:

How to wordwrap mysql data?

Whenever I try to wrap a mysql string with php like this:

while($row = mysql_fetch_array($result)){
    $id=$row['id'];
    $message = $row['message'];
    $wrappedmessage = wordwrap($message, 3, "\n", true);
    ...
}

It never wraps. It adds a space. Say for example the message is "Hello how are you." it would print "Hel lo how ar e yo u." Whats wrong?

ANSWER:

while($row = mysql_fetch_array($result)){
  $id=$row['id'];
  $message = $row['message'];
  $wrappedmessage = wordwrap($message, 3, "<br/>", true);

  echo "$wrappedmessage<br/>";
}

Upvotes: 9

Views: 4088

Answers (4)

Alex
Alex

Reputation: 4934

http://www.w3schools.com/PHP/func_string_wordwrap.asp

You should include a <br /> in the function call alongside \n, so it formats the HTML.

Upvotes: 1

GWW
GWW

Reputation: 44161

I am betting you are viewing the text through a webbrowser.

You'll need to use <br/> instead of \n as a line break or surround your text with <pre></pre> blocks.

EDIT:

Another alternative as suggested by Christian Sciberras in his comment is to use:

nl2br(wordwrap($message, 3, "\n", true));

Upvotes: 2

Adam Culp
Adam Culp

Reputation: 500

Is your output going to a browser? If so, you need to put "<br />\n". Otherwise if your output is at command line the "\n" is enough.

Upvotes: 0

Jon Abaca
Jon Abaca

Reputation: 841

Please try the PHP_EOL constant instead of "\n". It will generate the newline character for the system PHP is running on.

Upvotes: 0

Related Questions