在 PHP 中删除新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6766551/
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-26 01:11:18  来源:igfitidea点击:

Strip new lines in PHP

php

提问by pt2ph8

I've been working with phrases the past couple of days and the only problem I seem to be facing is stripping new lines in the html before printing it.

过去几天我一直在处理短语,我似乎面临的唯一问题是在打印 html 之前去除 html 中的新行。

Anybody have any idea how to remove everynew lines from HTML using PHP?

任何人都知道如何使用 PHP 从 HTML 中删除每个新行?

回答by pt2ph8

str_replace(array("\r", "\n"), '', $string)

回答by user319730

the pt2ph8answer didn't work for me until I have changed it a little bit as follows:

pt2ph8答案为我所做的,直到我已经改变了它一点点如下不起作用:

str_replace(array("\r", "\n"), '', $string);

You can see the different results in this demo:

您可以在此演示中看到不同的结果:

http://phpfiddle.org/lite/code/g5s8-nt3i

http://phpfiddle.org/lite/code/g5s8-nt3i

回答by cornips

Why not use regular expression like

为什么不使用像这样的正则表达式

$string = trim(preg_replace('/\s+/', ' ', $string));

as proposed here.

正如这里所提议的。