简体   繁体   English

在PHP和CodeIgniter中不使用nl2br打印新行

[英]New line not printing by using nl2br in PHP and CodeIgniter

I am trying to solve a new line issue in CodeIgniter. 我正在尝试解决CodeIgniter中的换行问题。 My variable is set as below 我的变量设置如下

$ba='hi\r\nhello';

When I try to print it like this 当我尝试像这样打印时

echo nl2br($ba);

It's not converting \\r\\n to a new line. 它不是将\\r\\n转换为新行。 After a whole day of searching I found out that this is because my variable is with ' not " 经过一整天的搜索,我发现这是因为我的变量带有' not "

I've also tried to use preg_replace but it's the same 我也尝试过使用preg_replace但是一样

preg_replace("/\r\n|\r|\n/",'<br/>',$ba);

But I can't change this so how do I fix this issue? 但我无法更改此设置,那么如何解决此问题?

Thanks for helping out. 感谢您的帮助。

"\\n" is not the same as '\\n' ! "\\n"'\\n' This is your basic problem. 这是您的基本问题。 You are not trying to replace what you think you are 您并没有试图取代您认为的自己

"\\n" is converted in to a line feed or ASCII 13 "\\n"转换为换行或ASCII 13

'\\n' is the character \\ then the character n '\\n'是字符\\然后是字符n

$ba='hi\r\nhello';
$ba=str_replace('\r\n','<br>',$ba);

echo $ba; // hi<br>hello

to use a regular expression: 使用正则表达式:

as \\ is an escape character you need to triple it: 由于\\是转义字符,因此需要将其增加三倍:

$ba=preg_replace('#\\\\\\r\\\\\\n|\\\\\\r|\\\\\\n#','<br/>',$ba);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM