2010-01-05 59 views
0

我希望有更好的解决问题能力的人可以帮助我。我有一个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

你看过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. 
+0

是'wordwrap' MB-安全吗?我很确定'nl2br'不是,对于这个''preg_replace('/ \ n/u','
',$ s)'是另一种选择。 – 2010-01-05 00:37:54

+0

1行代码!很高兴我来到这里,我会采取漫长的路线 - 谢谢你! – jimsmith 2010-01-06 01:37:24