How to split text to next line in php?

I have this project, which is a type of blog where I pull messages sent from the server. The problem is that when the message is echoed (ed) the text goes on one line, how do I get it to go to the next line after a certain length?

Here is the code in which the text is placed

$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 enter image description here

+4
source share
8 answers

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() .

+7
source
 $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 100 to your needs.

But wordwrap is the best solution :)

+4
source

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; ?> 
+3
source
 echo implode('<br/>',str_split($string,100)); 
+2
source

First, if you want the text to be as wide as possible, attach all this to the wrapping <div> element and use CSS for its maximum width. Any text inside an element followed by a wrapper.

Here is the CSS code you need:

 #mydiv {width:200px;} 

The only text that will still be the problem after this is the text without any spaces in it, which will still stretch from the edge of the page.

For this you can use another CSS property, word-wrap , for example:

 #mydiv {word-wrap:break-word;} 

The best practice is to keep the CSS code separate from your HTML, but if you are not using style sheets, you can add CSS code directly to the <div> element with the style attribute, for example:

 <div style='width:200px; word-wrap:break-word;'> ..... (your content goes here) ..... </div> 

Of course, it is possible to do word wrap in PHP using the wordwrap() (or str_split() function for long lines without spaces), but in the end you will have lines having different lengths when displayed on the page, because the font has different widths for different characters. Therefore, I would say that CSS solution is better, because the packaging of words will look better on the page.

Hope this helps.

+2
source

Edit: I initially thought this was a CSS issue. I see that you want to break lines that are very large with php. You can use wordwrap () for this, as recommended by artlung.

What I originally thought in CSS: put every blog post in a div and assign word-wrap: break-word; for the div, and also indicate the width of the div.

My idea on jsfiddle.com: Link

+1
source

this should do it:

 $newtext = wordwrap($text, 20, "<br />\n"); 
Examples

can be found here

+1
source
+1
source

All Articles