2
<?php
$fruits = "lemon
    orange
banana
apple
    pear";

$fruits_a = explode("\n", $fruits);
for ($i = 0; $i < count($fruits_a); $i++)
{
    $fruits_a[$i] = trim($fruits_a[$i]);
}
$fruits = implode(" ", $fruits_a);

echo $fruits;
?>

是否可以将其(从爆炸到内爆)转换为单行?在python中它只是fruits = " ".join(x.strip() for x in fruits.split("\n"))

4

2 回答 2

2
$fruits = implode(
    ' ',
    array_map(
        'trim',
        explode("\n", $fruits)
    )
);
于 2013-07-20T14:42:53.387 回答
1

是的

$fruits = implode(' ', array_map('trim', explode('\n', $fruits)));
于 2013-07-20T14:42:12.820 回答