-1

I have 2 php files named index.php and detail.php.

My porblem is that even thought this script writes to the text file correctly but when it writes content of detail.php it writes \n instead of the newline.

Expected Output:

A
B
C
D

I get:

A
B
C\nD

Here is the code for the files: index.php

<?php

$filename= 'logs/'.$d.'a.txt';
$extra=file_get_contents("detail.php");
$somecontent = "A\nB\n".$extra;

$handle = fopen($filename, 'a');
fwrite($handle, $somecontent);
fclose($handle);

?>

detail.php

<?php

echo 'C\nD';

?>

The result of file_get_contents is directly written as if copy-pasted, the \n chracter is not considered as newline.

What i have tried:

detail.php

=>echo 'C\r\nD'

=>echo 'C\\nD';

=>echo 'C\\r\\nD';

=>echo 'C'.PHP_EOL.'D';

Ashesh
  • 71
  • 2
  • 10
  • What OS are you running on? – D4V1D Aug 27 '15 at 08:44
  • 1
    [Difference between single quotes and double quotes](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php). Since everybody's saying use single quotes but nobody told you why you should use single quotes specifically. – Andrei Aug 27 '15 at 08:46
  • Hi, try this code `D"; ?>` – Mansoor H Aug 27 '15 at 08:47

2 Answers2

2

To use \n \r\n etc in strings you have to put them in doublequotes instead of single eg echo "C\r\nD";

donald123
  • 5,638
  • 3
  • 26
  • 23
0

Remove the singe quote marks and take double ones. ' -> "

pguetschow
  • 5,176
  • 6
  • 32
  • 45