开发者

How to remove trailing line-feed character while exploding

开发者 https://www.devze.com 2023-03-12 13:18 出处:网络
I have a plain text file with many lines. Each line contains 5 words exactly. Eg: cat bat rat mat sat hello hi howdy namaste salaam

I have a plain text file with many lines. Each line contains 5 words exactly. Eg:

cat bat rat mat sat
hello hi howdy namaste salaam
here there where nowhere somewhere
a b c d e
......
......

Now I am retrieving the lines one by one using fgets() function. The lines are retrieved perfectly, but when i explode the string to form an arra开发者_开发百科y of the 5 words, i see that the trailing line feed character is also included in the array.

So how to remove the trailing line feed character while exploding?


When you get the lines use rtrim

rtrim($myline, "\n")


Take a look at stream_get_line which removes the specified line ending.


rtrim($myline) is even shorter.

As stated in http://php.net/manual/en/function.rtrim.php:

Without the second parameter, rtrim() will strip these characters: " ", an ordinary space. "\t", a tab. "\n", a new line (line feed). "\r", a carriage return. "\0", the NULL-byte. "\x0B", a vertical tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消