SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to remove new line character?

    Hi,

    Please check the following text file:

    Code:
    http://www.configureweb.com/test.txt
    As you can see, it seems to have two lines as follows:

    1
    2

    Now, please download that text file and open it with Notepad (To download it, you can use "save page as" feature of the browser). You will see that it seems to have one line but there is an invisible new line character between 1 and 2. How can I remove this hidden character from this file using str_replace()?

    I tried the following separately with no success:

    PHP Code:
    $file_content str_replace(PHP_EOL''$file_content);
    $file_content str_replace('\r'''$file_content);
    $file_content str_replace('\n'''$website_content); 
    Thanks for any ideas.

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trim will remove these characters
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 2ndmouse View Post
    trim will remove these characters
    Thanks but trim doesn't work here. That character is not at the beginning or end of the string.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nayen View Post
    Thanks but trim doesn't work here. That character is not at the beginning or end of the string.
    You need to use double quotes instead of single quotes.
    PHP Code:
    $file_content str_replace(PHP_EOL''$file_content); 
    $file_content str_replace("\r"''$file_content); 
    $file_content str_replace("\n"''$website_content); 
    Single Quotes tell PHP to use exactly those characters, double quotes will parse those into return feed and line feed accordingly.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    You need to use double quotes instead of single quotes.

    Single Quotes tell PHP to use exactly those characters, double quotes will parse those into return feed and line feed accordingly.
    Thank you very much. I totally missed that. With double quotes it just works as I needed.

  6. #6
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried using Ascii Characters with str_replace()?

    \ = chr(92)
    n = chr(110)
    r = chr(114)
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •