如何将 newlines/break 行内爆到 space?

How to implode newlines/break lines to a space?

如何将 newlines/break 行内爆到 space?

给定这些 $strings:

The quick brown
fox jumps 
over the lazy
dog

用空格内爆那些字符串 space

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

我有这个:

The quick brownfox jumpsover the lazydog

而我正试图拥有这个:

The quick brown fox jumps over the lazy dog

有灯吗?谢谢。

你可以使用爆炸:

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

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

echo $strings;

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

首先你必须像这样分解字符串

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

然后对分解后的数据进行内爆

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

最后打印出来

echo $string;

我觉得效果不错:)

<?php

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

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

echo $strings;

?>

您可能想为此使用 preg_replace 而不是 explode/implode:

$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