PHP将2行转换为1行

时间:2014-11-24 12:46:50

标签: php

My son seems very interested.
 Do you know why my game started?

上面是一个字符串行,我试图将其形成为

My son seems very interested. Do you know why my game started?

我尝试了以下代码

$finish_loop = "no";

while($finish_loop=="no")
{

if(!strstr($dialogue,"  "))
{$finish_loop = "yes";}

$dialogue = str_replace("  "," ",$dialogue);
}

但$对话仍然将$对话作为2行返回,我只想要一行中的所有内容,我如何用php实现这一点

1 个答案:

答案 0 :(得分:0)

用空格替换文本中的新行,然后修剪它以删除superflus空格:

$dialogue = str_replace("\n\r"," ",$dialogue);
$dialogue = str_replace("\r"," ",$dialogue);
$dialogue = trim(str_replace("\n"," ",$dialogue));