English 中文(简体)
如何从字符串中删除换行符( 无字符!)?
原标题:How to remove line breaks (no characters!) from the string?

我搜索了SO和网络的其余部分, 以寻找我问题的答案, 最后又发现同样不足的“解决方案”。

我将用户输入从文字区域保存到 MySQL 数据库( 在 WordPress 环境中, 但不至于这个问题, 我相信) 。 它后来从 DB 中提取, 在网站后端显示给管理者。 当用户提交有线断开的文本( 即按 Enter 键) 时, 就会出现问题 。

样板字符串可能看起来是这样 :

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

Greetings,
Bill

There are no end of line characters (" ", " ", or the like) in the string.

我使用 nl2br () 生成 HTML 输出,但这还不够。 结果如下:

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

If the string had EOL characters such as " " in it, I d hit it with either str_replace() or preg_replace() and be done with it, but I have no clue what needle to feed either of those functions if there ain t no characters there in the first place.

我可以手动访问 DB 中的相关字段, 点击后空格, 每条线断线, 以及我以后想做的字符串工作。 所以我知道我需要上述格式 。

最佳回答

您应该能够将它替换为删除所有新线和运货返回的预览。 代码是 :

preg_replace( "/
|
/", "", $yourString );

Even though the characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.

问题回答

Ben s sublict 是可以接受的,但str_replace () 远远快于 < a href=""http://www.php.net/preg_replace" rel="noreferr" > preg_replace (

$buffer = str_replace(array("
", "
"),   , $buffer);

使用较少的CPU电力,减少世界二氧化碳排放量。

$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("
", "
"),   , $str);  // echo $str in a single line

适当操作Wind Windows I d 建议使用

$buffer = str_replace(["
", "
", "
"], "", $buffer);

" " - for Windows, " " - for Mac and " " - for Linux

某些功能性更强的东西(容易在任何地方使用):

function strip_carriage_returns($string)
{
    return str_replace(["

", "
", "
"],   , $string);
}

使用 PHPP_EOL 作为搜索替换参数也是个好主意! Kudos 。

str_ replace( PHP_EOL, 无效, $str); 重置( PHP_EOL, 无效, $str);

您也可以使用“http://php.net/manual/en/form.trim.php' rel=“noreferrer” 标题=“trim” >PHP Trim

此函数返回一个字符串, 从 str 的开头和结尾处删除空白。 如果没有第二个参数, trim () 将会剥去这些字符 :

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

我想我的回答为时已晚, 但我在使用JS的剧本时, 也有同样的额外线条问题,

这将在字符串中添加 和换行符 。

$text = nl2br($text);

此行将删除额外的行 。

$text = preg_replace("/
|
/", "", $text);

替换 , 替换为任何不需要的通用符号, 如星号 , 并在 DB 中插入 :

$text = str_replace("<br />","**",$text );

当我想在程序中使用它的时候, 我用它来替换

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

然后在程序中使用 $$text

我用三行线来做这个工作, 所以把“ 坚挺” $ < / 坚挺” 当作你的“ 东西 ” 。

$s=str_replace(chr(10),  ,$s);
$s=str_replace(chr(13),  ,$s);
$s=str_replace("
"),  ,$s);
  • Legend:

<强>chr(10) chr(10)

<强>chr( 13) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

______a new line

替代内建:'https://www.php.net/manual/en/form.trim.php' rel=“不跟随 noreferrer'>trim()

trim — Strip whitespace (or other characters) from the beginning and end of a string
Description ¶
trim ( string $str [, string $character_mask = " 	

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.
    "	" (ASCII 9 (0x09)), a tab.
    "
" (ASCII 10 (0x0A)), a new line (line feed).
    "
" (ASCII 13 (0x0D)), a carriage return.
    "" (ASCII 0 (0x00)), the NUL-byte.
    "x0B" (ASCII 11 (0x0B)), a vertical tab.

用于从不同类型的文本文件中删除换行符,但不处理 html 。

您可以使用 preg_replace 替换符合正则表达式的字符串部件, 例如 :

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

尝试了所有这些解决方案之后。 唯一可行的是( 超时代 - 然后替换 - 然后是 超时代) 。

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




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!