PHP,使用内爆添加换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21584164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:16:08  来源:igfitidea点击:

PHP, add a newline with implode

phpimplode

提问by user1708580

I'm trying to add a newline \n, in my foreach statement with implode.

我正在尝试\n在我的 foreach 语句中添加一个换行符。

My code:

我的代码:

$ga->requestReportData($profileId,array('country'),array('visits')); 
$array = array();
foreach($ga->getResults() as $result){ 
    $array[] = "['".$result->getcountry()."', ".$result->getVisits()."]"; 
} 
echo implode(",\n", $array);

I only get a comma and a space between my results. I want a comma and a newline.

我的结果之间只有一个逗号和一个空格。我想要一个逗号和一个换行符。

I am trying to get something like this:

我想得到这样的东西:

['Country', 'number'],

['Country', 'number'],

['Country', 'number']

['国家','数量'],

['国家','数量'],

['国家','数量']

However I I get this:

但是我得到了这个:

['Country', 'number'], ['Country', 'number'], ['Country', 'number']

['国家','数字'],['国家','数字'],['国家','数字']

Why does my \n not cause a newline?

为什么我的 \n 不会导致换行?

回答by Andy

I suspect it is because you are echoing the data to the browser and it's not showing the line break as you expect. If you wrap your implode in the the <pre>tags, you can see it is working properly.

我怀疑这是因为您将数据回显到浏览器,并且没有按预期显示换行符。如果您将内爆包裹在<pre>标签中,您可以看到它工作正常。

Additionally, your arguments are backwards on your implodefunction, according to current documentation. However, for historical reasons, parameters can be in either order.

此外,根据当前文档,您的参数在您的内函数上是倒退的。但是,由于历史原因,参数可以按任意顺序排列。

$array = array('this','is','an','array');
echo "<pre>".implode(",\n",$array)."</pre>";

Output:

输出:

this,
is,
an,
array

回答by boroboris

For cross-platform-compatibility use PHP_EOLinstead of \n.

对于跨平台兼容性使用PHP_EOL而不是\n.

Using the example from the accepted answer above:

使用上面接受的答案中的示例:

$array = array('this','is','another','way');
echo "<pre>".implode(PHP_EOL, $array)."</pre>";

If you're writing directly to HTML (it wouldn't work on files) there is an option of using <br>like this:

如果您直接写入 HTML(它不适用于文件),则可以使用<br>以下选项:

$array = array('this','is','another','way');
echo "<p>".implode(<br>, $array)."</p>";

Both output:

两个输出:

this, 
is, 
another, 
way

回答by B.K

This can also work

这也可以工作

$array = array('one','two','three','four');
echo implode("<br>", $array);

Output:

输出:

one
two
three
four

回答by kelunik

Many others claim you use the wrong order, that's only partial right, because the docs only recommend this, but you don't have to:

许多其他人声称您使用了错误的顺序,这只是部分正确,因为文档只推荐了这一点,但您不必:

implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.

由于历史原因,implode() 可以按任一顺序接受其参数。然而,为了与explode() 保持一致,使用记录的参数顺序可能不会那么混乱。

I think your problem is caused by how browsers interpret HTML. They don't care about newlines, they're like a normal space for them.

我认为您的问题是由浏览器如何解释 HTML 引起的。他们不关心换行符,他们就像一个普通的空间。

To show these linebreaks, you can use <pre><?php echo implode($glue, $array); ?></pre>. You could also use nl2br(implode(..))or nl2br(implode(..), true)if you're writing XHTML.

要显示这些换行符,您可以使用<pre><?php echo implode($glue, $array); ?></pre>. 您也可以使用nl2br(implode(..))ornl2br(implode(..), true)如果您正在编写 XHTML。