Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Seeing special characters

    I was just working with a string that I needed to break on new lines into array values (used preg_split for it), and initially, I just broke on \n, which actually lead to the artifact of having a hidden \r remaining I didn't notice. Upon echoing the variables, I saw a space, and wondered how it got in there. After a bit of thought, I realized that was the \r I couldn't see.

    I know I could have used trim to get rid of the problem in the first place, but I'm curious: is there a way to have new characters show up? I tried addslashes to no avail. There are ways to avoid the issue of course, but just for my own information, I'm wondering if anyone has a method I could use?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The only way that makes sense for this is to walk the string array through a ord() call character by character. \r = 0xD and \n = 0xA. Knowing this you could replace it then with \ + r and \ + n, which could actually be done using an str_replace now that I think of it.

  • Users who have thanked Fou-Lu for this post:

    Keleth (09-13-2011)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Sorry, a bit of a necro, and I apologize, as I forgot I asked this...

    so I can have str_replace search for the hex values of the characters? I need to find a chart, I don't remember hex values anymore

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    If you just want newlines and carriage returns visible you can do something like this:
    PHP Code:
    $myString "Some text\r\nmore text";
    echo 
    str_replace(array("\r""\n"), array('\r''\n'), $myString); 
    That will output:
    Code:
    Some text\r\nmore text

  • Users who have thanked Inigoesdr for this post:

    Keleth (09-13-2011)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Awesome, thanks!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, I've been kinda busy myself.
    I was thinking the only way to do it is to ord it and compare, and then I had the after thought of simply replacing "\r\n" with '\r\n', as demonstrated by Inigoesdr.


  •  

    Posting Permissions

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