如何在 PHP 中将字符串从多行转换为单行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1701339/
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
How to Transform a String from Multi-Line to Single-Line in PHP?
提问by edt
Is there a PHP string function that transforms a multi-line string into a single-line string?
是否有将多行字符串转换为单行字符串的 PHP 字符串函数?
I'm getting some data back from an API that contains multiple lines. For example:
我从包含多行的 API 获取一些数据。例如:
<p>Some Data</p>
<p>Some more Data</p>
<p>Even More Data</p>
I assign that data to a variable, then echo the variable as part/"cell" of a CSV document.
我将该数据分配给一个变量,然后将该变量作为 CSV 文档的一部分/“单元格”进行回显。
It's breaking my CSV document. Instead of all content showing in one cell (when viewing in OpenOffice Calc), it shows in multiple cells and rows. It should be contained within one cell.
它正在破坏我的 CSV 文档。所有内容都显示在一个单元格中(在 OpenOffice Calc 中查看时),它显示在多个单元格和行中。它应该包含在一个单元格中。
I would like to transform the string into:
我想将字符串转换为:
<p>Some Data</p><p>Some more Data</p><p>Even More Data<p>
Or, what is the best fix for this?
或者,对此的最佳解决方法是什么?
回答by Peter Lindqvist
Line conversion techniques
线转换技术
Approach 1
方法一
To remove anything unnecessary between the closing and opening </p>...<p> tags you can use a regular expression. I haven't cleaned it up so it's just for reference.
要删除关闭和打开 </p>...<p> 标记之间不需要的任何内容,您可以使用正则表达式。我没有清理过,仅供参考。
$str = preg_replace("/(\/[^>]*>)([^<]*)(<)/","\1\3",$str);
It will strip anything between the p tags, such as newlines, whitespace or any text.
它将删除 p 标签之间的任何内容,例如换行符、空格或任何文本。
Approach 2
方法二
And again with the delete-only-linebreaks-and-newlines approach
再次使用仅删除换行符和换行符的方法
$str = preg_replace("/[\r\n]*/","",$str);
Approach 3
方法三
Or with the somewhat faster but inflexible simple-string-replacement approach
或者使用更快但不灵活的简单字符串替换方法
$str = str_replace(array("\r","\n"),"",$str);
Take a pick!
挑一个!
Comparison
比较
Let's compare my methods
让我们比较一下我的方法
Performance
表现
Performance is always relative to the fastest approach in this case the second one.
在这种情况下,性能总是相对于最快的方法第二个。
(Lower is better)
(越低越好)
Approach 1 111
Approach 2 300
Approach 3 100
Result
结果
Approach 1
Strips everything between tags
方法 1
剥离标签之间的所有内容
Approach 2 and 3
Strips newline and linebreak characters
方法 2 和 3
去除换行符和换行符
回答by Ben Everard
This will remove line breaks only, you obviously don't want to remove spaces as this would apply to the string within your paragraph tags.
这将仅删除换行符,您显然不想删除空格,因为这将应用于段落标记中的字符串。
$str = str_replace(array("\n", "\r"), '', $str);
回答by Richard
The best method I found to make a multi-line string a single line was like this
我发现使多行字符串成为单行的最佳方法是这样的
$newstring = str_replace(array("\r\n", "\n", "\r"), '', $oldstring);
This is similar to other answers but adds "\r\n", which must be the initial part of the array, so that it doesn't double up.
这与其他答案类似,但添加了“\r\n”,它必须是数组的初始部分,以免加倍。
回答by defines
You simply need to remove all new line (\n) and carriage return (\r) characters from the string. In PHP this is as simple as:
您只需从字符串中删除所有新行 (\n) 和回车 (\r) 字符。在 PHP 中,这很简单:
$string = str_replace(array("\n", "\r"), '', $string);

