5

I try to avoid asking stupid questions, but this for some reason is just not working. I'm retrieving some text from a database, with newlines included. When using echo straight away, this is the output:

=== BOLD TEXT ===\nHere is some text.

This is how i retrieved it:

$row = $query->row();
$content=$row->course_content;

Neither of the following have any effect.....

$content= str_replace(array("\r\n", "\r", "\n"), "<br />", $content); 
$content= str_replace("\n", " ", $content);
$content = nl2br($content);

HOWEVER, when i hard code it in a string to the original statement,

$content="=== BOLD TEXT ===\nHere is some text.";

pang! it works... any ideas as to why it refuses to accept the database input, but is fine with the manual string?

Complete Code:

//Switch between these two:
$row = $query->row();
$content=$row->course_content;
$content="=== BOLD TEXT ===\nHere is some text.";
$content = str_replace(array("\r\n", "\r", "\n"), "<br />", $content); 
$content = str_replace("\n", " ", $content);
$content = nl2br($content);     
echo $content;
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
MaxQ
  • 595
  • 1
  • 8
  • 25
  • What is the variable type of `course_content` in your database? – christopher Mar 07 '13 at 17:27
  • It's a longtext with utf8_general_ci Could that be the issue? – MaxQ Mar 07 '13 at 17:31
  • Have you tried converting it to plain old String and seeing what happens? Because PHP is loosely typed, it's sometimes hard to keep track of the types that are moving around the code. – christopher Mar 07 '13 at 17:31
  • no, there was no result from changing it to a normal text :( – MaxQ Mar 07 '13 at 17:35
  • If you get `\n` in your echo output, then you've probably got literal "\" and `n` in your text, NOT a newline. Try "\\n" as your replace chars instead. – Marc B Mar 07 '13 at 17:52

2 Answers2

5

When you do this:

$row = $query->row();
$content=$row->course_content;
echo $content;

Do you actually see the \n printed to the output? Or does it actually print a new line character?

If it's the former, then you have an escaped newline in your database string instead of an actual newline character. In other words, your database string contains two characters \ and n, which does not a newline make.

Try this replacement instead (or in addition to, to be completely safe):

$content = str_replace(array('\r\n', '\r', '\n'), "<br />", $content); 

The single quotes syntax for the string causes php to not turn the two characters \ and n into a newline character, thus it should actually match what's in your database. (You could also use \\n, where the first slash escapes the second slash, preventing it from turning into a newline.)

Edit: To answer your question, assuming you meant mysql_real_escape_string, then yes, this was expected behavior. From the documentation:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \ , ', " and \x1a.

Patrick M
  • 10,547
  • 9
  • 68
  • 101
  • Thanks, it works! But is it 'normal', or have i done something fishy in the database? i used real_escape_string before i inserted it. – MaxQ Mar 07 '13 at 17:42
3
$content="=== BOLD TEXT ===\nHere is some text.";

If you want to replace the \n, you should search for it using single quotes - '\n' (instead of "\n") - the two have very different meanings in PHP.

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • yes! That solved it. But why didn't nl2br($content) do anything? – MaxQ Mar 07 '13 at 17:37
  • @Manfred, `nl2br` looks for new line characters. You don't seem to have new line characters in your string. – Emanuil Rusev Mar 07 '13 at 17:45
  • Thanks, it works! But is it 'normal' this way, or have i done something fishy in the database? i used real_escape_string before i inserted it. – MaxQ Mar 07 '13 at 17:46
  • @Manfred, Yes, maybe you called `real_escape_string` twice for the same string. – Emanuil Rusev Mar 07 '13 at 17:53
  • @Manfred, you should see http://stackoverflow.com/questions/3171711/double-mysql-real-escape-string-insert-of-new-line-characters-how-to-get-new-li – Emanuil Rusev Mar 07 '13 at 17:54