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)?
Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
Remove some extra ones? The common ways to remove line breaks in PHP are: $nobreak = str_replace(["\r", "\n"], "", $STRING); $nobreak = preg_replace("/\r|\n/", "", $STRING);
trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs. The trim() method does not change the original string, it returns a new string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With