簡體   English   中英

如何從字符串中刪除換行符(無字符?)?

[英]How to remove line breaks (no characters!) from the string?

這似乎是一個騙局,但 rest 保證它不是 - 我已經搜索了 SO 以及 web 的 rest 來尋找我的問題的答案,並最終一遍又一遍地找到同樣不充分的“解決方案”。 無論如何,它是這樣的:

我正在將用戶輸入從文本區域保存到 MySQL 數據庫(在 WordPress 環境中,但我相信這對這個問題應該無關緊要)。 它稍后從數據庫中檢索,以顯示給站點后端的管理員。 當用戶提交帶有換行符的文本(即按下 Enter 鍵)時,就會出現問題。

示例字符串可能如下所示:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!

Greetings,
Bill

字符串中沒有行尾字符(“\n”、“\r”等)。

我在其上使用nl2br()生成 HTML output,但這還不夠。 結果是:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill

據我了解,哪個是預期的nl2br()結果,因為它插入了標簽並且不應該首先替換換行符?

但是我需要的格式是這樣的:

Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br /><br />Greetings,<br />Bill

如果字符串中有 EOL 字符,例如“\n”,我會用str_replace()preg_replace()命中它並完成它,但我不知道如果有的話,用什么針來喂養這些函數中的任何一個首先那里沒有字符。

我可以手動訪問數據庫中的相關字段,為每個換行符點擊 Backspace 以及我稍后想要對字符串進行的操作。 所以我知道我需要上面的格式。

Ben 的解決方案是可以接受的,但str_replace()preg_replace()快得多

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

使用更少的 CPU 功率,減少世界二氧化碳排放量。

您應該能夠用刪除所有換行符和回車符的 preg 替換它。 代碼是:

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

即使\\n字符沒有出現,如果你得到回車,那里有一個不可見的字符。 preg 替換應該抓住並修復這些。

$str = "
Dear friends, I just wanted so Hello. How are you guys? I'm fine, thanks!<br />
<br />
Greetings,<br />
Bill";

echo str_replace(array("\n", "\r"), '', $str);  // echo $str in a single line

這是因為nl2br()根本不刪除新行。

返回所有換行符( \\r\\n\\n\\r\\n\\r之前插入<br /><br>字符串。

使用str_replace代替:

$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);

str_replace(PHP_EOL, null, $str);

您也可以使用PHP 修剪

此函數返回一個字符串,其中從 str 的開頭和結尾去除了空格。 如果沒有第二個參數,trim() 將去除這些字符:

  • " " (ASCII 32 (0x20)),一個普通的空格。
  • "\\t" (ASCII 9 (0x09)),一個制表符。
  • "\\n" (ASCII 10 (0x0A)),換行(換行)。
  • "\\r" (ASCII 13 (0x0D)),回車。
  • "\\0" (ASCII 0 (0x00)),NUL 字節。
  • "\\x0B" (ASCII 11 (0x0B)),一個垂直制表符。

為了在 Windows 上也能正常工作,我建議使用

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

"\\r\\n" - 適用於 Windows, "\\r" - 適用於 Mac 和"\\n" - 適用於 Linux

功能更強大的東西(易於在任何地方使用):

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

使用PHP_EOL作為搜索替換參數也是一個好主意! 榮譽。

我使用 3 行來完成這項工作,因此將$s視為您的“東西”...

$s=str_replace(chr(10),'',$s);
$s=str_replace(chr(13),'',$s);
$s=str_replace("\r\n"),'',$s);
  • 傳奇:

chr(10) ___a 換行

chr(13) ___返回

\\r\\n ______換行

替代內置: trim()

trim — Strip whitespace (or other characters) from the beginning and end of a string
Description ¶
trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string

This function returns a string with whitespace stripped from the beginning and end of str.
Without the second parameter, trim() will strip these characters:

    " " (ASCII 32 (0x20)), an ordinary space.
    "\t" (ASCII 9 (0x09)), a tab.
    "\n" (ASCII 10 (0x0A)), a new line (line feed).
    "\r" (ASCII 13 (0x0D)), a carriage return.
    "\0" (ASCII 0 (0x00)), the NUL-byte.
    "\x0B" (ASCII 11 (0x0B)), a vertical tab.

它可以從不同類型的文本文件中刪除換行符,但不處理 html。

已解決且運行良好

我認為我的答案為時已晚,但在 JS 腳本中使用它時遇到了相同的額外行問題,但它的工作原理是這樣的

  $text     = nl2br($text);   // it will add <br /> and line breaks in string
   
  
 $text = preg_replace( "/\r|\n/", "", $text );// this line will remove extra line, 
 

 $text = str_replace("<br />","**",$text );  replcing <br /> with any unwanted common symbol like this astreaks "**" while inserting in DB
 


and when I want to use it in program that time I use it replacing **

 $text = str_replace("**","\\n",$text );

然后在程序中使用 $text

您可以使用 preg_replace 來替換匹配正則表達式的字符串部分,例如:

preg_replace("/\s+/","",$string);

在嘗試了所有這些解決方案之后......唯一有效的是(urlencode - 然后替換 - 然后 urldecode)

$jsonDump = urlencode($jsonDump);
$jsonDump = str_replace("%5Cr","",$jsonDump);
$jsonDump = str_replace("%5Cn","",$jsonDump);
$jsonDump = urldecode($jsonDump);

暫無
暫無

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

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