如何破坏换行符/断行到空格?

时间:2015-02-28 17:31:09

标签: php regex string algorithm implode

如何将换行/断行内容压缩到空格?

给出这些$ strings:

The quick brown
fox jumps 
over the lazy
dog

使用空格插入这些字符串

$keys = implode(' ', array_keys($strings));

我有这个:

The quick brownfox jumpsover the lazydog

我正试着这个:

The quick brown fox jumps over the lazy dog

有什么灯吗?谢谢。

4 个答案:

答案 0 :(得分:4)

你可以使用explode:

$strings = <<<TEXT
The quick brown
fox jumps 
over the lazy
dog
TEXT;

$strings = implode(' ', explode("\n", $strings));

echo $strings;

如果您需要更多解释,请告诉我:)

答案 1 :(得分:2)

首先你必须像这样爆炸字符串

$var='The quick brown
fox jumps 
over the lazy
dog';
$data=explode(' ',$var);

然后内爆爆炸数据

$string=implode(' ',$data);

最后打印出来

echo $string;

我认为它运作良好:)

答案 2 :(得分:2)

<?php

$strings = "The quick brown
fox jumps 
over the lazy
dog
";

$strings = implode(' ', array_map( 'trim', explode("\n", $strings)));

echo $strings;

?>

答案 3 :(得分:1)

您可能希望使用preg_replace代替爆炸/内爆:

$s = 'The quick brown
fox jumps
over the lazy
dog';

$s = preg_replace('#[\r\n]#', ' ', $s);

echo $s;

输出:

The quick brown fox jumps over the lazy dog