Php "newline" Character Printing Fails - All the output is on one line

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Php "newline" Character Printing Fails - All the output is on one line

jlhaslip
I am just learning to code some php and have been fighting this problem that should be so simple, but I am having a real problem with it.
Since I am new to the language, and I haven't done much coding lately, I am inserting 'echo' or 'print' statements into the script I am writing so that the script can tell me whether the contents of a couple of variables are in fact being altered during the script. This tells me whether the script is functioning as I think it should at various times, so I can 'de-bug' the code.

BUT, I can't seem to get the 'new line' or 'carriage return' escaped properly. I have been using "\n\r" inside single quotes, double quotes, no quotes, a variable name, and still the output is all in one long string which is not easy to read. I even went to phpbuilder.com and snagged a line from their tutorial and can't get the output to print onto new lines for me in order to make the debugging simpler.

I'm thinking the correct code should be:
CODE
echo "this should be \n\n a new line here \n\rand here, too";

but it all comes out without the line feeds. As I said above, I've tried single quotes, double quotes, variables, no qoutes, concatenations, but they all fail...

Example output: a long string all run together:
QUOTE
flat_file_data.txtThis spans multiple lines. The newlines will be output as well.this should be a new line here and here, tooThis spans multiple lines. The newlines will be output as well.No forced echo... we have a handle/home/jlhaslip/public_html/testsidebar/flat_file_data.txtartfold @ artfold %line here/n/rartfold1 @ artfold1 %1line here/n/rartfold2 @ artfold2 %2line here/n/rartfold3 @ artfold3 %3line here/n/r @ %4line here/n/r


Any suggestions?

 

 

 


Reply

Hamtaro
What you need is the <br> HTML tag before each newline character. HTML doesn't accept newlines. So, for example (using the example you used):
CODE
echo "this should be <br>\n<br>\n a new line here <br>\n and here, too";

You won't need to do the carriage return for a new line in the page.

Reply

jlhaslip
QUOTE
flat_file_data.txt
No forced echo... we have a handle
/home/jlhaslip/public_html/testsidebar/flat_file_data.txtartfold
artfold
line here/n/rartfold1
artfold1
1line here/n/rartfold2
artfold2
2line here/n/rartfold3
artfold3
3line here/n/r

4line here/n/r


Thanks... topic being closed.

Reply

iGuest
How to display test in saperate line
Php \"newline\" Character Printing Fails

Respected Sir,
I have writen a php file as below.
But I got mail in one line.
What is a coading to desplay all variable in new(saperate) line.
Pls help me.

$msg = $_REQUEST['name'];
$msg .= $_REQUEST['dob'];
$msg .= $_REQUEST['organization'];
$msg .= $_REQUEST['designation'];
$msg .= $_REQUEST['department'];
$msg .= $_REQUEST['email'];
$msg .= $_REQUEST['mobile'];
$msg .= $_REQUEST['tel'];
$msg .= $_REQUEST['fax'];
$msg .= $_REQUEST['name1'];
$msg .= $_REQUEST['email1'];
$msg .= $_REQUEST['name2'];
$msg .= $_REQUEST['email2'];
$msg .= $_REQUEST['name3'];
$msg .= $_REQUEST['email3'];
$msg .= $_REQUEST['name4'];
$msg .= $_REQUEST['email4'];
$message = str_replace("\and", "<br/>", $msg);
$headers = "From:$name";
Mail( "info@automationpltd.Com", "Feedback Form", $message, $headers);

Regards,

Ashish Panchal

-question by Ashish Panchal

Reply

iGuest
new line character appears correctly in browser but it does not print in file.
Php \"newline\" Character Printing Fails

In the above code the new line character appears correctly in the browser but when I try to replace the word text which is written in the file certificate.Rtf with the string $text, new line characters have not any effect and the tag <br> appears in the file new_certificate.Rtf as text.
File certificate.Rtf is a simple rtf file contains only the word text.

Echo "this should be <br>\and<br>\and a new line here <br>\and and here, too";

$text="this should be <br>\and<br>\and a new line here <br>\and and here, too";

$filename = 'certificate.Rtf';

$filename2 = 'new_certificate.Rtf';


// open our template file
$fp = fopen ( $filename, 'are' );

if (!$fp)
{
echo "Error in file opening!";
echo "<br>";
}

//generate the headers to help a browser choose the correct application
header( 'Content-Disposition: filename=cert.Rtf');
header( 'Content-type: msword' );

//read our template into a variable
$output = fread( $fp, filesize( $filename ) );

// replace the place holders in the template with our data
$output = str_replace( 'text', $text, $output );

if (file_exists($filename2)) {
$fp2 = fopen($filename2, 'are+');
} else {
$fp2 = fopen($filename2, 'x+');
}

if (!$fp2)
{
echo "Error in file2 opening!";
echo "<br>";
}

fwrite($fp2, $output);

fclose($fp);

fclose ( $fp2 );

Thank you in advance!

-question by Marios Vlachos

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for php, newline, character, printing, fails, output, line
advertisement



Php "newline" Character Printing Fails - All the output is on one line



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE