Reputation: 131
i have this project which is blog type where I pull posts submitted from the server. The problem is that when the post is echo(ed) the text goes all in one line , how do I make it go to the next line after certain length?
Here is the code where the text is being posted
$postpost = $result["post_user"];
echo " <b>name</b> = " .$result["username"] ."<br>";
echo " <b>post</b> = " .$result["post"] . "<br>";
echo " <b>date</b> = ".$date["date"]. "
<br><br>----------------------<br><br>";
and here is the output problem
Upvotes: 4
Views: 5875
Reputation: 5426
Edit: I initially thought this is a CSS problem. I see that you want to break strings which are very large with php. You can use wordwrap() for that, as artlung recommended.
What I initially thought in CSS: put each blog entry into a div, and assign word-wrap: break-word; for the div and also specify width of the div.
My idea on jsfiddle.com: Link
Upvotes: 1
Reputation: 168685
Firstly, if you want the text to have a maximum width, enclose the whole thing in a wrapping <div>
element, and use CSS to style it to a maximum width. Any text inside the element with then wrap nicely.
Here's the CSS code you'd need:
#mydiv {width:200px;}
The only text that will still be a problem after that is text without any spaces in it, which would still stretch off the edge of the page.
For these, you can use another CSS property, word-wrap
, like so:
#mydiv {word-wrap:break-word;}
The best practice is to keep your CSS code separate from your HTML, but if you're not using stylesheets, you can add the CSS code directly to the <div>
element with the style
attribute, like so:
<div style='width:200px; word-wrap:break-word;'>
..... (your content goes here) .....
</div>
It is, of course, possible to do word-wrapping in PHP, using it's wordwrap()
function (or str_split()
for long strings with no spaces), but you'll end up with the lines being varying lengths when displayed on the page, because the font has different widths for different characters. Therefore, I would say that the CSS solution is better because the word wrapping will look better on the page.
Hope that helps.
Upvotes: 2
Reputation: 34013
Use wordwrap()
:
// the 80 is the number of characters after which to break:
echo " <b>post</b> = " . wordwrap($result["post"], 80, '<br>') . '<br>';
You may also be interested in nl2br()
.
Upvotes: 9
Reputation: 3020
$postpost = $result["post_user"];
echo " <b>name</b> = " .$result["username"] ."<br>";
echo " <b>post</b> = " .chunk_split($result["post"],100,"<br/>") . "<br>";
echo " <b>date</b> = ".$date["date"]. "
<br><br>----------------------<br><br>";
This will break it after 100 characters. You can change the 100 to your needs.
But wordwrap is a better solution :)
Upvotes: 5
Reputation: 2664
Use php's wordwrap() function.
http://php.net/manual/en/function.wordwrap.php
Upvotes: 1
Reputation: 3754
this should do it :
$newtext = wordwrap($text, 20, "<br />\n");
examples can be found here
Upvotes: 1
Reputation: 5058
You can use wordwrap function in php
<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>
Upvotes: 3