0

I have a string which is returned by a web service and contains the new line characters, "\n". When I try to output the string with the nl2br function, the string is returned as is, with "\n" inside the string.

Could it be possible that the new line character needs to have some special encoding to be identified as such or am I missing something trivial here?

Hex representation of the input string

48617570747374726173736520315c6e416e737072656368706172746e65723a204d6178204d75737465726d616e6e
sectus
  • 15,605
  • 5
  • 55
  • 97
Awemo
  • 875
  • 1
  • 12
  • 25
  • 1
    Maybe it's literal `\n` string sequence instead of New Line character? – Justinas Feb 10 '15 at 08:11
  • `nl2br` will add the `br` and it will also _keep_ the original new lines. Is that the issue? – Salman A Feb 10 '15 at 08:17
  • May we look at hex representation of this string? (bin2hex) – sectus Feb 10 '15 at 08:18
  • @Justinas, you may be right about it being a literal string sequence. The new line sequence is correctly displayed when I edit the string. – Awemo Feb 10 '15 at 08:39
  • @SalmanA No, that is not the problem. The whole string is treated as if no lines were present. – Awemo Feb 10 '15 at 08:41
  • possible duplicate of [How to remove line breaks (no characters!) from the string?](http://stackoverflow.com/questions/10757671/how-to-remove-line-breaks-no-characters-from-the-string) – kenorb Mar 03 '15 at 00:29
  • This question is not a duplicate of the other other question. My problem was that I had a literal `\n` string sequence while that other question was trying to convert actual line breaks. The problem the OP for the other question had was the fact that `nl2br()` adds a `
    ` tag to the line breaks while my problem was that `nl2br()` did not change the string at all due to the literal `\n`.
    – Awemo Mar 03 '15 at 09:53

3 Answers3

2
48617570747374726173736520315c6e416e737072656368706172746e65723a204d6178204d75737465726d616e6e

According to your hex string it seems that your string does not contain newline. It just \n - two bytes 5c6e where 5c is \ and 6e is n. But new line is 0a

echo (bin2hex("\n")); // 0a

You could replace \n with <br /> using str_replace.

$string = hex2bin('48617570747374726173736520315c6e416e737072656368706172746e65723a204d6178204d75737465726d616e6e');
echo str_replace('\n', "<br/>\n", $string);
// please note, that '\n' and "\n" are different in PHP, '\n' - just two symbols, "\n" - one new line symbol.

Some code: http://3v4l.org/N0FjC#v540

sectus
  • 15,605
  • 5
  • 55
  • 97
1

to replace all linebreaks to br tag the best solution is:

<?php 
function nl2br2($string) { 
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string); 
return $string; 
} 
?> 

because each OS have different ASCII chars for line-break:

windows = \r\n 
unix = \n 
mac = \r 
vivex
  • 2,496
  • 1
  • 25
  • 30
  • 2
    ... Or replace your array with `PHP_EOL`. – rdiz Feb 10 '15 at 08:16
  • This could be a possible solution, what however puzzles me is the reason why the character is not recorgnized by the original nl2br function. – Awemo Feb 10 '15 at 08:43
0

nl2br — Inserts HTML line breaks before all newlines in a string

It doesn't replace newlines with line breaks, it inserts it before.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
  • It does neither in this case, the string is printed out without any HTML line breaks at all. – Awemo Feb 10 '15 at 08:42
  • Sorry about that. Are you outputting the string in the browser or in the console? Check that it's not being html encoded by looking at the browser source. – dan-klasson Feb 10 '15 at 09:03