Re: CR at the end of line !



Daniel Moyne wrote:
Hello !
I load lines written sequentially in a text file with this php command :
$line=fgets($fp,4096);
but I collect also the expected 'xOA' character at the end of the file
line ; how can I get rid of this nasty CR char ? ; I tried rtrim
 unsuccessfully so far with this command ! :
$aa=rtrim($line);
Regards

From:tbm.at.home.dot.nl
Windows uses two characters for definining newlines, namely ASCII 13
(carriage return, "\r") and ASCII 10 (line feed, "\n") aka CRLF. So if
you have a string with CRLF's, trim() won't recognize them as being one
newline. To solve this you can use str_replace() to replace the CRLF's
with with a space or something.

<?php
// string with bunch of CRLF's
$my_string = "Liquid\r\nTension Experiment\r\n\r\n\r\n";

// replace CRLF's with spaces
$my_wonderful_string = str_replace("\r\n", " ", $my_string);
// would result in "Liquid Tension Experiment  "

// or just delete the CRLF's (by replacing them with nothing)
$my_wonderful_string = str_replace("\r\n", "", $my_string);
// would result in "LiquidTension Experiment"
?>


.



Relevant Pages

  • Re: CR at the end of line !
    ... I load lines written sequentially in a text file with this php command: $line=fgets; but I collect also the expected 'xOA' character at the end of the file line; how can I get rid of this nasty CR char? ... Windows uses two characters for definining newlines, ... you have a string with CRLF's, trimwon't recognize them as being one newline. ...
    (alt.php)
  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)