views:

192

answers:

1

I hope somebody with better problem solving skills can help me out here. I have a textarea and all I had to do is split the text into 50 chars and feed the lines to another app. No problem. But I forgot about \n line breaks. If someone puts linebreak I have to also make that a seperate line. Here's my current code ($content is the original text). I'm sure there is an easy way I just can't get to it.

        $div = strlen($content) / 50;


        $x=0;
        while ($x<$div) {

        $substr=$x*50;

        $text = substr($content,$substr,50);


        if (trim($text)!="") {

                   echo $text . "<br>";

              }

        $x++;


              }
+1  A: 

Have you looked into PHP's wordwrap and nl2br functions?

$result = wordwrap($content, 50, "\n"); // first, wrap
$result = nl2br($result); // then, include html breaks


So, this:

$content = <<<EXAMPLE
hope somebody with
better problem solving skills can help me out here.

I have a textarea and all I had to do is split the text into 50 chars and feed the lines to another app. 
EXAMPLE;

...yields this:

hope somebody with<br />
better problem solving skills can help me out<br />
here.<br />
<br />
I have a textarea and all I had to do is split<br />
the text into 50 chars and feed the lines to<br />
another app. 
Derek Illchuk
is `wordwrap` mb-safe? i'm pretty sure `nl2br` is not, for which `preg_replace('/\n/u', '<br>', $s)` is an alternative.
fsb
1 line of code! so glad I came here I would have taken the long route - thank you!
jimsmith