我有一个带有换行符的字符串,我想打印出来,以便我看到特殊字符(如'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);
?>
可以使用addcslashes函数:
string addcslashes ( string $str , string $charlist )
返回一个字符前带反斜杠的字符串。
实现这一点的一种方法是
echo str_replace("'n", ''n', $yourstring);
当然,还有很多其他的方法,你可以赋值而不是回显,并增强你的代码等等。
考虑新行可能会根据底层操作系统的不同而被不同地处理。
还可以考虑阅读关于新行和载波返回的理论背景