簡體   English   中英

從字符串中刪除新行並替換為一個空格

[英]Remove new lines from string and replace with one empty space

$string = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

想要從字符串中刪除所有新行。

我有這個正則表達式,它可以捕獲所有這些,問題是我不知道我應該使用哪個函數。

/\r\n|\r|\n/

$string應該變成:

$string = "put returns between paragraphs for linebreak add 2 spaces at end ";

你必須小心雙換行符,這會導致雙空格。 使用這個非常有效的正則表達式:

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

多個空格和換行符被替換為一個空格。

編輯:正如其他人指出的那樣,此解決方案存在匹配單詞之間的單個換行符的問題。 這在示例中不存在,但可以很容易地看到這種情況是如何發生的。 另一種方法是執行以下操作:

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

對接受的答案的一些評論:

+表示“1 個或多個”。 我認為您不需要重復\\s 我認為你可以簡單地寫'/\\s+/'

此外,如果要刪除字符串中的第一個和最后一個空格,請添加trim

通過這些修改,代碼將是:

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

只需使用preg_replace()

$string = preg_replace('~[\r\n]+~', '', $string);

你可以在這個上使用str_replace() ,盡管代碼看起來不那么干凈:

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

ideone觀看直播

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

您應該使用 str_replace 來提高速度並在數組中使用雙引號

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

我不確定這對已經提交的答案是否有任何價值,但我也可以發布它。

// Create an array with the values you want to replace
$searches = array("\r", "\n", "\r\n");

// Replace the line breaks with a space
$string = str_replace($searches, " ", $string);

// Replace multiple spaces with one
$output = preg_replace('!\s+!', ' ', $string);

轉義序列\\R匹配通用換行符

也就是說,任何被 Unicode 視為換行符的序列 這包括與\\v垂直空白)匹配的所有字符,以及多字符序列\\x0D\\x0A ...

$string = preg_replace('/\R+/', " ", $string);

在 8 位非 UTF-8 模式下, \\R等效於以下內容: (?>\\r\\n|\\n|\\x0b|\\f|\\r|\\x85) ... pcre.org

Regex101 演示

PCRE 正則表達式替換可以使用 preg_replace 完成: http : //php.net/manual/en/function.preg-replace.php

$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);

將用空格替換新行或返回字符。 如果您不想替換它們,請將第二個參數更改為''

使用這個:

用空字符串替換一系列換行符:

$string = preg_replace("/[\\n\\r]+/", "", $string);

或者您可能想用一個空格替換換行符:

$string = preg_replace("/[\\n\\r]+/", " ", $string);

我很驚訝地看到每個人都對正則表達式知之甚少。

在 php 中去除換行符是

$str = preg_replace('/\r?\n$/', ' ', $str);

在 perl 中

$str =~ s/\r?\n$/ /g;

意思是用空格替換行尾的任何換行符(為了效率) - 可選地前面有一個回車符 - 。

\\n 或 \\015 是換行符。 \\r 或 \\012 是回車。 ? 在正則表達式中表示匹配前一個字符的 1 或 0。 正則表達式中的 $ 表示匹配行尾。

原始和最好的正則表達式參考是 perldoc perlre,每個編碼人員都應該非常了解這個文檔: http ://perldoc.perl.org/perlre.html 注意並非所有功能都支持所有語言。

文本中的換行符通常表示為:

\\r\\n - 在 Windows 計算機上

\\r - 在蘋果電腦上

\\n - 在 Linux 上

//Removes all 3 types of line breaks

$string = str_replace("\r", "", $string);

$string = str_replace("\n", "", $string);

關於:

$string = trim( str_replace( PHP_EOL, ' ', $string ) );

這應該是一個非常強大的解決方案,因為如果我沒有錯,\\n 不能在所有系統中正常工作......

這是我會使用的模式

$string = preg_replace('@[\s]{2,}@',' ',$string);

這個也刪除了標簽

$string = preg_replace('~[\r\n\t]+~', '', $text);

您可以嘗試以下代碼將保留文本中的任何空白和新行。

$str = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

echo preg_replace( "/\r|\n/", "", $str );

也許這有效:

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

許多這些解決方案對我不起作用。 不過,這確實奏效了:-

$svgxml = preg_replace("/(*BSR_ANYCRLF)\R/",'',$svgxml);

這是參考:- PCRE 和新行

很簡單

$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C

下面的代碼適用於所有文本,請使用它:

$des = str_replace('\n',' ',$des);
$des = str_replace('\r',' ',$des);

您可以刪除新行和多個空格。

$pattern = '~[\r\n\s?]+~';
$name="test1 /
                     test1";
$name = preg_replace( $pattern, "$1 $2",$name);

echo $name;

使用上述解決方案的組合,這一行對我有用

 $string = trim(str_replace('\n', '', (str_replace('\r', '', $string))));

它刪除了 '\\r' 和 '\\n'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM