如何使用它输出字符串';php中的换行符是否可见?

如何使用它输出字符串';php中的换行符是否可见?,php,string,echo,Php,String,Echo,我有一个包含新行的字符串,我想打印出来,这样我就可以看到特殊字符(例如\n)而不是新行本身 所以一个字符串通常是echo'd作为 this is the top line of my string this is the second line of the string 而是像这样: this is the top line of my string\nthis is the second line of the string 如何在php中实现这一点?尝试以下方法: <?php

我有一个包含新行的字符串,我想打印出来,这样我就可以看到特殊字符(例如
\n
)而不是新行本身

所以一个字符串通常是
echo
'd作为

this is the top line of my string
this is the second line of the string
而是像这样:

this is the top line of my string\nthis is the second line of the string
如何在php中实现这一点?

尝试以下方法:

<?php

$string = "this is the top line of my string
this is the second line of the string";

echo json_encode($string);

?>

试试这个:

<?php

$string = "this is the top line of my string
this is the second line of the string";

echo json_encode($string);

?>

您可以使用以下功能:

string addcslashes ( string $str , string $charlist )
它将返回一个字符前带有反斜杠的字符串。

您可以使用以下函数:

string addcslashes ( string $str , string $charlist )

它将返回一个在字符前带有反斜杠的字符串。

实现这一点的一种方法是

echo str_replace("\n", '\n', $yourstring);
当然,还有很多其他的方法,您可以分配而不是回音,并增强代码等等

考虑到新线可能会根据底层操作系统进行不同的处理

<>也考虑阅读

的理论背景
实现这一目标的一个方法是

echo str_replace("\n", '\n', $yourstring);
当然,还有很多其他的方法,您可以分配而不是回音,并增强代码等等

考虑到新线可能会根据底层操作系统进行不同的处理

<>也考虑阅读

的理论背景
addslashes()
或将\n替换为\\n.Hmm。。。奇怪的是,
addslashes()
对我不起作用,但它似乎就是我想要的。使用addslashes()仍然会给我一个出现在多行上的字符串。@johncorser的确切输出是什么:
var\u dump($yourString)取自源代码?确切的变量转储如下:
addslashes()
或用\\n.Hmm替换\n。。。奇怪的是,
addslashes()
对我不起作用,但它似乎就是我想要的。使用addslashes()仍然会给我一个出现在多行上的字符串。@johncorser的确切输出是什么:
var\u dump($yourString)取自源代码?确切的var_转储是这样的:这为我所需要的工作。好主意!始终保持它的干净和简单;)这正是我所需要的。好主意!始终保持它的干净和简单;)