Jump to content
xisto Community
Sign in to follow this  
jlhaslip

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

Recommended Posts

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:

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:

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?
Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

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):

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.

Share this post


Link to post
Share on other sites

flat_file_data.txtNo 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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Further toHamtaro's answer...

If you do not want to add BR's in the string you could wrap your output in the PRE html tag. This will tell most browsers to display everything inside as preformatted text which maintains newlines.

 I.E.

$string = "stuffand more stuff on another line"; 
echo '<pre>'.$string.'</pre>';

 

 

 

-reply by Author Name - e.G. John, MikeKeywords: windows linux new line issues email string php

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.