Home  >  Article  >  Backend Development  >  Several ways to replace line breaks in php

Several ways to replace line breaks in php

怪我咯
怪我咯Original
2017-07-12 14:55:182273browse

The newline character is a computer language expression whose function is to jump to the next new line. In different languages, the code is also different. For example: 0c6dc11e160d3b678d68754cc175188a, "\n", "endl", etc. This article mainly introduces several methods of replacing line breaks in PHP

The first one:

<?php 
?$str="this is a test \n"; 
$patten = array("\r\n", "\n", "\r"); 
?//先替换掉\r\n,然后是否存在\n,最后替换\r 
$str=str_replace($order, "", $str); 
?>
//php 有三种方法来解决 

//1、使用str_replace 来替换换行 
$str = str_replace(array("\r\n", "\r", "\n"), "", $str); 

//2、使用正则替换 
$str = preg_replace(&#39;//s*/&#39;, &#39;&#39;, $str); 

//3、使用php定义好的变量 (建议使用) 
$str = str_replace(PHP_EOL, &#39;&#39;, $str);
/* 
* 获得用户操作系统的换行符,\n 
* @access public 
* @return string 
*/ 
function get_crlf() 
{ 
if (stristr($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Win&#39;)) 
{ 
$the_crlf = &#39;\r\n&#39;; 
} 
elseif (stristr($_SERVER[&#39;HTTP_USER_AGENT&#39;], &#39;Mac&#39;)) 
{ 
$the_crlf = &#39;\r&#39;; // for old MAC OS 
} 
else 
{ 
$the_crlf = &#39;\n&#39;;//权重大一点 
} 
return $the_crlf; 
}

Note: When the front page is displayed, use nl2br to make line breaks Become 0c6dc11e160d3b678d68754cc175188a

The second example:

Found an interesting thing:

$text="aaaa

ccc";

$text=str_replace('\n',"",$text);
$text=str_replace('\r',"",$text);
$text=str_replace('\r\n',"",$text);

Normally speaking, the above code should be able to replace the newline character

But in fact But it’s not possible!

I’m very depressed. I tried many times but it just doesn’t work.

Finally changed to this

The code is as follows:

$text=str_replace("\n","",$text); 
$text=str_replace("\r","",$text); 
$text=str_replace("\r\n","",$text);

Everything is OK~~It turns out to be a problem of double quotes and single quotes! !

Double quotation marks are less efficient than single quotation marks, because when double quotation marks are parsed by PHP, they will also judge whether there are variables in them, but single quotation marks will not make this judgment, so generally speaking, there is no Whenever variables are involved, I always use single quotes. I didn’t expect that replacing the newline character this time would not work with single quotes...

Finally written in one sentence

The code is as follows:

$order = array("\r\n", "\n", "\r"); 
$replace = &#39;&#39;; 
$text=str_replace($order, $replace, $text);

This way you can replace the newline character!

The above is the detailed content of Several ways to replace line breaks in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn