如何避免PHP wordwrap删除空格

如何避免PHP wordwrap删除空格,php,string,formatting,icalendar,word-wrap,Php,String,Formatting,Icalendar,Word Wrap,对于我正在使用的iCal生成器,我需要确保每75个字符中有一个字符串如下所示: $string = "This is a long text. I use this text to demonstrate the PHP wordwrap function."; $newstring = wordwrap($string, 75, "\r\n ", TRUE); echo($newstring); This is a long text. I use this text to demonst

对于我正在使用的iCal生成器,我需要确保每75个字符中有一个字符串如下所示:

$string = "This is a long text. I use this text to demonstrate the PHP wordwrap function.";
$newstring = wordwrap($string, 75, "\r\n ", TRUE);

echo($newstring);
This is a long text. I use this text to demonstrate the PHP wordwrapfunction.
结果:

This is a long text. I use this text to demonstrate the PHP wordwrap
 function.
iCal将第一个空格(来自wordwrap break参数)解释为文本属性继续的指示器

wordwrap函数删除了第二个空格(从字符串中)。因此,在解码iCal内容后,文本将如下所示:

$string = "This is a long text. I use this text to demonstrate the PHP wordwrap function.";
$newstring = wordwrap($string, 75, "\r\n ", TRUE);

echo($newstring);
This is a long text. I use this text to demonstrate the PHP wordwrapfunction.

我怎样才能解决这个问题?我不想删除字符串中的空格(在“wordwrap”和“function”之间)。

我必须使用
chunk\u split
。它将保留空间,也不会试图在我不需要的空间包装

$string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaannnnnn";
$newstring = rtrim(chunk_split($string, 75, "\r\n "), "\r\n ");

echo($newstring);
保留的空间:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  aaaaannnnnn
rtrim
也被使用,因为
chunk\u split
总是附加
end


但是,这不会计算
end
中的空间。因此,如果有多行,一行实际上可能有76个字符长。我将
chunklen
参数更改为74,因为这对于我的用例来说已经足够好了。

好的,在执行wordwrap时传递一个额外的空间,以便保留wordwrap和函数之间的空间

用这个

$newstring = wordwrap($string, 75, " \r\n", TRUE);
而不是

$newstring = wordwrap($string, 75, "\r\n", TRUE);
iCal之前的输出:

This is a long text. I use this text to demonstrate the PHP wordwrap 
function.
iCal后的输出:

This is a long text. I use this text to demonstrate the PHP wordwrap function.