Home >Backend Development >PHP Problem >How to replace newline characters in php

How to replace newline characters in php

藏色散人
藏色散人Original
2020-07-27 09:12:373722browse

How to replace newlines in php: 1. Use the "str_replace" function that comes with php to replace newlines; 2. Use regular expressions to replace newlines; 3. Use variables defined by php to Implement replacement line breaks.

How to replace newline characters in php

Recommendation: "PHP Tutorial"

PHP replace newline character

First type:

The code is as follows:

<?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);

The code is as follows:

/*
* 获得用户操作系统的换行符,\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 displaying the front page, use nl2br The newline becomes 0c6dc11e160d3b678d68754cc175188a

Second example description:

I found an interesting thing:

$text="aaaa
ccc";
$text=str_replace(&#39;\n‘,"",$text);
$text=str_replace(&#39;\r‘,"",$text);
$text=str_replace(&#39;\r\n‘,"",$text);

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

But in fact it is 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 will replace the newline character!

The above is the detailed content of How to replace newline characters 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