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.

4
  • You forgot to include your code.. Commented Jul 2, 2012 at 22:44
  • Possible duplicate of stackoverflow.com/questions/3760816/… Commented Jul 2, 2012 at 22:46
  • your using preg_replace with an incorrect regular expression. Probably easier to use one of the examples of str_replace() that @Corbin and I describe below, for what you are doing Commented Jul 3, 2012 at 0:00
  • I didn't forget... n00bs can be b00bs. Sorry. Commented Jul 3, 2012 at 0:17

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

8 Comments

I'm confused now if OP wants to replace NL or the literal '\\n' rather.
might want to edit this '\n' === "\\n" === '\\n' to just this '\n' === "\\n"
@JoeCortopassi the three are actually equal, though it's obviously not valid PHP syntax.
@JoeCortopassi One part of my brain can sees the logic of it, and then the other part of my brain goes, "What the--?!" :)
@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])
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.