Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't replace \n in php with content from database

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;
like image 421
MaxQ Avatar asked Mar 07 '13 17:03

MaxQ


People also ask

What is the \n in PHP?

Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.

How to print break line PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

How to echo line break PHP?

Answer: In PHP to echo a new line or line break use '\r\n' or '\n' or PHP_EOL. '\n' or '\r\n' is the easiest way to embed newlines in a PHP string. Also, '\n' can be used to create line breaks when creating PHP text files.

How do you delete a line in PHP?

The line break can be removed from string by using str_replace() function.


2 Answers

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.

like image 127
Patrick M Avatar answered Oct 26 '22 22:10

Patrick M


$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.

like image 34
Emanuil Rusev Avatar answered Oct 26 '22 23:10

Emanuil Rusev