php 如何从php字符串中删除新行并返回?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3986299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:39:08  来源:igfitidea点击:

how to remove new lines and returns from php string?

phpstringnewline

提问by Yens

A php variable contains the following string:

一个 php 变量包含以下字符串:

<p>text</p>
<p>text2</p>
<ul>
<li>item1</li>
<li>item2</li>
</ul>

I want to remove all the new line characters in this string so the string will look like this:

我想删除此字符串中的所有新行字符,因此该字符串将如下所示:

<p>text</p><p>text2><ul><li>item1</li><li>item2</li></ul>

I've tried the following without success:

我尝试了以下但没有成功:

str_replace('\n', '', $str);
str_replace('\r', '', $str);
str_replace('\r\n\', '', $str);

Anyone knows how to fix this?

有谁知道如何解决这个问题?

回答by codaddict

You need to place the \nin double quotes.
Inside single quotes it is treated as 2 characters '\'followed by 'n'

您需要将 放在\n双引号中。
在单引号内,它被视为 2 个字符'\'后跟'n'

You need:

你需要:

$str = str_replace("\n", '', $str);

A better alternative is to use PHP_EOLas:

更好的选择是PHP_EOL用作:

$str = str_replace(PHP_EOL, '', $str);

回答by kapa

You have to wrap \nor \rin "", not ''. When using single quotes escape sequences will not be interpreted (except \'and \\).

你必须包装\n\r"",而不是''。使用单引号时,不会解释转义序列(\'和除外\\)。

The manual states:

该手册指出

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

  • \nlinefeed (LF or 0x0A (10) in ASCII)

  • \rcarriage return (CR or 0x0D (13) in ASCII)\

  • (...)

如果字符串用双引号 (") 括起来,PHP 将为特殊字符解释更多的转义序列:

  • \n换行符(ASCII 中的 LF 或 0x0A (10))

  • \r回车(ASCII 中的 CR 或 0x0D (13))\

  • (……)

回答by tfont

Something a bit more functional (easy to use anywhere):

功能更强大的东西(易于在任何地方使用):

function replace_carriage_return($replace, $string)
{
    return str_replace(array("\n\r", "\n", "\r"), $replace, $string);
}

Using PHP_EOLas the search replacement parameter is also a good idea! Kudos.

使用PHP_EOL作为搜索替换参数也是一个好主意!荣誉。

回答by Asif Mulla

This should be like

这应该像

str_replace("\n", '', $str);
str_replace("\r", '', $str);
str_replace("\r\n", '', $str);

回答by saravanavelu

To remove new lines from string, follow the below code

要从字符串中删除新行,请按照以下代码

$newstring = preg_replace("/[\n\r]/","",$subject); 

回答by Jonathan

Correct output:

正确的输出:

'{"data":[{"id":"1","reason":"hello\nworld"},{"id":"2","reason":"it\nworks"}]}'

function json_entities( $data = null )
{           
    //stripslashes
    return str_replace( '\n',"\"."\n",
        htmlentities(
            utf8_encode( json_encode( $data)  ) , 
            ENT_QUOTES | ENT_IGNORE, 'UTF-8' 
        )
    );
}

回答by rvandoni

you can pass an array of strings to str_replace, so you can do all in a single statement:

您可以将一个字符串数组传递给str_replace,因此您可以在一条语句中完成所有操作:

$content = str_replace(["\r\n", "\n", "\r"], "", $content);

回答by Collin Anderson

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

回答by Alagu Sundar

Replace a string :

替换字符串:

$str = str_replace("\n", '', $str);

u using also like, (%n, %t, All Special characters, numbers, char,. etc)

u 也使用类似 (%n, %t, All Special characters, numbers, char,. etc)

which means any thing u can replace in a string.

这意味着您可以在字符串中替换任何东西。