Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string replace doesn' work with double quote qualifier 1

Status
Not open for further replies.

jmoeller18

Programmer
Oct 9, 2001
12
0
0
US
using the string replace function and change any new lines into html breaks

and $fb=str_replace('\n', &quot;<br><br>&quot;, $fb);

this works - single quote qualifier

'Success\n\n'

this doesn't - double quote qualifier

&quot;Success\n\n&quot;

Why?


Jeffrey E. Moeller
 
[In my answer, I'm going to be very explicit with my use of single- and double-quotes.]


Your problem is because of PHP's string-interpolation rules.

In PHP, &quot;\n&quot; and '\n' are not the same thing.

&quot;\n&quot; is the single newline character, because escaped characters are interpolated inside double-quoted string literals in PHP.

'\n' is the literal string &quot;<backslash>n&quot; (two characters), because escaped characters are not interpolated inside single quotes in PHP.

In your invocation of str_replace(), you use '\n' as your &quot;search-for&quot; string, so you're telling PHP to look for &quot;<backslash>n&quot; in the string &quot;Success<newline><newline>&quot;.

'<backslash>n' does not appear in &quot;Success<newline><newline>&quot;. '<backslash>n<backslash>n', however, does appear twice in 'Success<backslash>n<backslash>n'.



I would also like to draw your attention to the special-purpose PHP builtin function, nl2br() ( It's designed to do what you're doing right now.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top