1

http://php.net/manual/en/function.trim.php

The manual entry does say that I can remove any specified leading or trailing characters with trim. I am reading in an array with the file() function. Each element of the array read in has a trailing line break.

I used trim($entry," \t\n\r") in attempt to remove these line breaks, but nothing useful seems to be happening. When I echo "[".trim($entry,' \t\n\r')."]\n";, I see-

[host2*2012-03-29 22:38:47 *129.118.243.193
]
[host1*2012-03-29 22:48:16*129.118.243.194
]

What am I missing out?

Lord Loh.
  • 2,437
  • 7
  • 39
  • 64
  • 2
    Try `trim($entry, " \t\n\r");` double quotes are essential (something to do with interpretation of the escape character). – Halcyon Mar 29 '12 at 23:28
  • @Frits van Campen - I used the single quotes after I failed with double quotes. I have put the double quotes again, but there is no difference! – Lord Loh. Mar 29 '12 at 23:41
  • https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php#comment35408611_3446245 – mickmackusa Mar 12 '22 at 14:43

3 Answers3

4
" \t\n\r"

not

' \t\n\r'

Note the double quotes

See the relevant page of the PHP manual for explanation of the difference between single and double quoted strings.

Also, take a look at the FILE_IGNORE_NEW_LINES option for the flags argument when using file()

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I think 'FILE_IGNORE_NEW_LINES' is going to be most helpful, but I still want to know how to get rid of line breaks using trim() – Lord Loh. Mar 29 '12 at 23:43
1

Just replace

trim($entry,' \t\n\r')

with

trim($entry," \t\n\r")

This is a string delimiter problem. But please not that if the line-break is on tow characters (like "\r\n") then it won't we totally deleted.

Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • I used the single quotes after I failed with double quotes. I have put the double quotes again, but there is no difference! I did not understand the 'tow characters' part of what you explained. If it is about the unix / windows CRLF or just LF, trim should have removed it, right? – Lord Loh. Mar 29 '12 at 23:44
  • 1
    You're right. My mistake. But if your bug is not solved, you should check what exactly is the last character of your string. `$ord = ord($entry[strlen($entry)-1])` $ord must be 10 or 13. – Skrol29 Mar 29 '12 at 23:59
0

use double quote instead so that the characters will be interpreted properly.

johnshen64
  • 3,734
  • 1
  • 21
  • 17
  • I used the single quotes after I failed with double quotes. I have put the double quotes again, but there is no difference! – Lord Loh. Mar 29 '12 at 23:41
  • Does chop work for your? It may be enough if it works. Not sure why it would not work with trim though. – johnshen64 Mar 30 '12 at 00:11