php trim() 是否删除换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3727839/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:54:42  来源:igfitidea点击:

Does php trim() remove line breaks?

php

提问by muncherelli

I am creating an application that takes a "swipe" from a magnetic reader. The string that is returned from the magnetic reader has a line break after the text. Can I use php trim() to remove the linebreak (and obviously any pre or post whitespace)?

我正在创建一个从磁性阅读器“刷卡”的应用程序。从磁性阅读器返回的字符串在文本后有一个换行符。我可以使用 php trim() 删除换行符(显然是任何前后空格)吗?

回答by Pekka

Yes it does, see the manual:

是的,请参阅手册

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.

此函数返回一个字符串,其中从 的开头和结尾去除了空格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)),一个垂直制表符。

回答by Petah

Also note: trim($string, "\n")or trim($string, " ")

另请注意:trim($string, "\n")trim($string, " ")

回答by Snowman

Note that trim will not remove it by default, if it has breakline: <br>at the end, so be careful with that.

请注意,默认情况下修剪不会删除它,如果它breakline: <br>在最后,所以要小心。

Otherwise @Pekka's @Petah's answer is good.

否则@Pekka 的@Petah 的回答很好。