1

When I do a str_replace for \n it keeps replacing all the letters "n" in my script. What I am trying to do it replace this "see\nyou\nsoon\n" to say "see you soon". Except it is stripping every n in the sentence. I keep getting this "see you soo".

Is it possible to target, specifically "\n" and not "n"?

Simple question. Perhaps nimrodic. Thanks for your time & solutions.

EDIT > Adding Code > This is the code I am trying to fix

    $text = "Description: Join us today, Sunday644 \u2022 Pastor Ed Weiss \u2022 Faith World\nOutreach Church \u2022 Bartlett, IL \u2022 Starting at 11:30am,\nfwo.org\/webcast<http:\/\/www.google.com\/url?q=http:\/\/fwo.org\/webcast&ust=1340553597624000&usg=AFQjCNE5g0jUGFl1nDLmKOHuIG8M8y3Wcg>\n";
    preg_match("/Description: ([^\\r\\n]+)/s", $text, $b);
    $text = preg_replace("/\u2022/", "•", $text);
    $text = preg_replace("/[\n\r]/"," ",$text);

    echo $text;

In my testing I am loosing the "r" and "n" letters.

2 Answers2

7

A new line and an n are two completely different characters.

New line is almost always represented as \n in programming languages, however, it is actually just character code 10 (decimal). n, on the other hand, is character code 110 (decimal). Other than the escape sequence, there is no relation at all between them.

To replace \n and only \n use:

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

Edit 1: For an example, see the snippet posted by Conner in the comment.

Edit 2: As noted by JoeCortopassi, PHP will only parse \n inside of certain types of string literals. For example, '\n' will end up in a literal \n string. (Character \ followed by character n).

"\n" //new line (char code 10)
'\n' //literal \ followed by n (char code 92 followed by char code 110)
"\\n" //literal \ followed by n (same as '\n')
'\\n' //literal \ followed by n (same as '\n' :p)

Or:

"\n" === chr(10)
'\n' === "\\n" === '\\n' === chr(92) . chr(110)
Corbin
  • 33,060
  • 6
  • 68
  • 78
  • 1
    I'm confused now if OP wants to replace NL or the literal `'\\n'` rather. – mario Jul 02 '12 at 22:48
  • might want to edit this `'\n' === "\\n" === '\\n'` to just this `'\n' === "\\n"` – JoeCortopassi Jul 02 '12 at 23:24
  • 1
    @JoeCortopassi the three are actually equal, though it's obviously not valid PHP syntax. – Corbin Jul 02 '12 at 23:39
  • Lol, just tested it and you're right. Php's warts never cease to amaze me – JoeCortopassi Jul 02 '12 at 23:43
  • 1
    @JoeCortopassi One part of my brain can sees the logic of it, and then the other part of my brain goes, "What the--?!" :) – Corbin Jul 02 '12 at 23:56
  • Yup :-) I bet there is a perfectly logical explanation how they decided it was a good idea – JoeCortopassi Jul 03 '12 at 00:02
  • Check it out @Corbin, here's the legitimate reason for it: http://stackoverflow.com/a/11302847/251012 – JoeCortopassi Jul 03 '12 at 00:11
  • 1
    @JoeCortopassi Ah! Somehow the escaping of single quotes escaped me. All I could think was "wait... nothing needs to be escaped in single quotes!" haha. (Though I wouldn't go as far as to say there's usually legit reasons for their design oddities :p [this is actually not a design oddity though]) – Corbin Jul 03 '12 at 02:09
3

Option #1

If you have a string that is being displayed like so:

see\nyou\nsoon\n

...then you will want to use this:

$str = str_replace('\n', ' ', $string);

...because PHP will is displaying the backslash character followed by the character for 'n'. You use single quotes, because php interprets that as a literal string with no escaped characters.

`

Option #2

If, on the other hand, you have a string that is being displayed like so:

see
you
soon

...then you will want to use this:

$str = str_replace("\n", '', $string);

...because php will look at this string as something that needs to be interpreted. This means that variables will be processed, and escaped characters like \n, \t and \\ will be interpreted into their literal character values.

From what you're talking about, it sounds like your strings are litered with what should be new line statements, but that were never interpreted, so option #1 is probably you're best bet

JoeCortopassi
  • 5,083
  • 7
  • 37
  • 65