Removing newline characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Varia

    Removing newline characters

    How would you remove newline characters from a string?

    I tried

    str_replace("\n ", "", $string);

    But that doesn't work.



  • Agelmar

    #2
    Re: Removing newline characters

    you also need to remove \r from the string.
    (on windows, newline is \r\n, on *nix it's just \n, but a windows client
    will send \r\n, and on older macs I think newline was just \r but I can't
    remember for sure.)


    "Amit Varia" <spam@amitvaria .com> wrote in message
    news:buvh93$595 $1@geraldo.cc.u texas.edu...[color=blue]
    > How would you remove newline characters from a string?
    >
    > I tried
    >
    > str_replace("\n ", "", $string);
    >
    > But that doesn't work.
    >
    >
    >[/color]


    Comment

    • CountScubula

      #3
      Re: Removing newline characters

      "Amit Varia" <spam@amitvaria .com> wrote in message
      news:buvh93$595 $1@geraldo.cc.u texas.edu...[color=blue]
      > How would you remove newline characters from a string?
      >
      > I tried
      >
      > str_replace("\n ", "", $string);
      >
      > But that doesn't work.
      >
      >
      >[/color]

      as was posted there is also the \r on some files or \r\n, try this:

      // for end of line (or begining)
      $string = trim($string);

      // from everywhere
      $string = str_replace("\n ", "", $string);
      $string = str_replace("\r ", "", $string);

      --
      Mike Bradley
      http://www.gzentools.com -- free online php tools


      Comment

      Working...