使用换行符将文本拆分为批次

时间:2010-01-05 00:21:54

标签: php string

我希望有更好解决问题能力的人可以帮助我。我有一个textarea,我所要做的就是将文本分成50个字符并将这些行提供给另一个应用程序。没问题。但我忘记了\ n换行符。如果有人放线,我还必须把它分开。这是我当前的代码($ content是原始文本)。我确信有一种简单的方法我无法达到它。

        $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 个答案:

答案 0 :(得分:1)

您是否查看了PHP的wordwrapnl2br函数?

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


所以,这个:

$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;

......产生这个:

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.