SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Bloomington, IL
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to remove newlines with ereg and hex value?

    I need to remove newlines from a string using the hex value for newline. The string may contain "\n", and I do not want to remove those, only newlines in the string. For instance, this is my string:
    Code:
    Hello
    
    
    World\n\nHow are you today?
    How would I remove the newlines under "Hello" while leaving the "\n" in tact after "World"? I figured I could use the hex value of NL, but this does not work:
    Code:
    echo ereg_replace("\x0A", ' ', $string);
    It just returns the original string with the newlines after "Hello" in tact. Any ideas?

    Thanks,
    Nathan

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Bloomington, IL
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. This is the code I used:
    Code:
    echo preg_replace("/[\n\r]/", '', $string);
    The \n's won't be removed unless I use \\n.

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah
    Part of a pattern that is in square brackets is called a "character class".
    In a character class the only meta-characters are:

    \ general escape character
    ^ negate the class, but only if the first character
    - indicates character range
    ] terminates the character class
    my mobile portal
    ghiris.ro


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
  •