php 使用php正则表达式从字符串中删除换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1991198/
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
Remove newline character from a string using php regex
提问by coderex
How can I remove a new line character from a string using php?
如何使用php从字符串中删除换行符?
采纳答案by Harmen
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);
回答by AntonioCS
$string = str_replace(PHP_EOL, '', $string);
or
或者
$string = str_replace(array("\n","\r"), '', $string);
回答by timmz
To remove several new lines it s recommended to use a regular expression:
要删除几个新行,建议使用正则表达式:
$my_string = trim(preg_replace('/\s\s+/', ' ', $my_string));
回答by Bhavesh Gangani
Better to use,
更好用,
$string = str_replace(array("\n","\r\n","\r"), '', $string).
$string = str_replace(array("\n","\r\n","\r"), '', $string).
Because some line breaks remains as it is from textarea input.
因为从 textarea 输入中保留了一些换行符。
回答by tfont
Something a bit more functional (easy to use anywhere):
功能更强大的东西(易于在任何地方使用):
function strip_carriage_returns($string)
{
return str_replace(array("\n\r", "\n", "\r"), '', $string);
}
回答by dkellner
Let's see a performance test!
来看看性能测试吧!
Things have changed since I last answered this question so here's a little test I created. I compared the 4 most promising methods, preg_replace vs strtr vs str_replace, and strtr goes twice because it has a single character and an array-to-array mode.
自从我上次回答这个问题以来,情况发生了变化,所以这是我创建的一个小测试。我比较了 4 种最有前途的方法,preg_replace 与 strtr 与 str_replace,并且 strtr 进行了两次,因为它具有单个字符和数组到数组模式。
You can run the test here:
您可以在此处运行测试:
https://deneskellner.com/stackoverflow-examples/1991198/
https://deneskellner.com/stackoverflow-examples/1991198/
Results
结果
251.84 ticks using preg_replace("/[\r\n]+/"," ",$text);
81.04 ticks using strtr($text,["\r"=>"","\n"=>""]);
11.65 ticks using str_replace($text,["\r","\n"],["",""])
4.65 ticks using strtr($text,"\r\n"," ")
(note that it's a realtime test and server loads may change so you'll probably get different figures)
(请注意,这是一个实时测试,服务器负载可能会发生变化,因此您可能会得到不同的数字)
The preg_replacesolution is noticeably slower but that's okay, they do a different job and PHP has no prepared regex so it's parsing the expression every single time. It's simply not fair to expect them to win.
该preg_replace解决方案明显变慢,但没关系,他们做不同的工作,PHP 没有准备好的正则表达式,所以它每次都解析表达式。期望他们获胜是不公平的。
On the other hand, in line 2-3, str_replaceand strtrare doing almost the same job and they perform quite differently. They deal with arrays, they do exactly what we told them - removethe newlines, replacing them with nothing.
在另一方面,在第2-3行,str_replace和strtr正在做的几乎相同的工作,他们的表现完全不同。他们处理数组,他们完全按照我们告诉他们的去做——删除换行符,用空替换它们。
The last one is a dirty trick: it replaces characters with characters, that is, newlines with spaces. It's even faster, and it makes sense because when you get rid of line breaks you probably don't want to concatenate the word at the end of one line with the first word of the next. So it's not exactlywhat the OP described, but it's clearly the fastest. With long strings and many replacements, the difference will grow because character substitutions are linear by nature.
最后一个是一个肮脏的技巧:它用字符替换字符,即用空格替换换行符。它甚至更快,这是有道理的,因为当您摆脱换行符时,您可能不想将一行末尾的单词与下一行的第一个单词连接起来。所以这并不完全是 OP 所描述的,但它显然是最快的。对于长字符串和许多替换,差异会增加,因为字符替换本质上是线性的。
Verdict: str_replacewins in general
判决:str_replace总体上获胜
And if you can afford to have spaces instead of [\r\n], use strtrwith characters. It works twice as fast in average case and probably a lot faster when there are many short lines.
如果您能负担得起空格而不是[\r\n],请使用strtr字符。在平均情况下,它的工作速度是原来的两倍,当有很多短行时,它的工作速度可能要快得多。
回答by Robert Sinclair
stripcslashesshould suffice (removes \r\n etc.)
stripcslashes应该就足够了(删除 \r\n 等)
$str = stripcslashes($str);
Returns a string with backslashes stripped off. Recognizes C-like \n, \r ..., octal and hexadecimal representation.
返回一个去掉反斜杠的字符串。识别类似 C 的 \n、\r ...、八进制和十六进制表示。
回答by Muhammed Suhail
Try this out Its working for me
试试这个它对我有用
First remove nfrom string (use double slash before n),
首先n从字符串中删除(在 之前使用双斜线n),
Then remove rfrom string like n
然后r从字符串中删除n
CODE :
代码 :
$string=str_replace("\n",$string);
$string=str_replace("\r",$string);
回答by Atul Baldaniya
function removeP($text){
$key=0;
$newText="";
while ($key < strlen($text)) {
if(ord($text[$key])==9 or ord($text[$key])==10){
//$newText .= '<br>';//uncomment this if you want <br> to replace that spacial characters;
}else{
$newText .= $text[$key];
}
// echo $k."'".$t[$k]."'=". ord($t[$k])."<br>";
$key++;
}
return $newText;
}
$myvar = removeP("your String");
Note:Here i am not using php regex but still you can remove the newline character
注意:这里我没有使用 php regex 但你仍然可以删除换行符
This will remove all newline characters which are not removed from by preg_replace, str_replace or trim functions
这将删除所有没有被 preg_replace、str_replace 或 trim 函数删除的换行符

