Html with php new line

php :: new line in textarea?

actually im doing echo $contact->address; To get it to work I need to do str_replace(‘\n’, «\n», $contact->address) which is retarded. Why is that?

How are you storing the data. Something is getting lost in translation. Do you add_slashes ? Save to a database?

@Hailwood. You must have a literal ‘\n’ then. That is, two characters, a backslash and an en, not a newline character (which is represented as «\n» for convenience, but it’s really just one char; to use that syntax you must use double quotes; same goes for tabs, for instance, \t ).

How are you getting the \n , are you typing it as two distinct characters, or pressing the enter key? More precisely, what’s the origin of your data?

12 Answers 12

Without seeing your code I cannot be sure, but my guess is you are using single quotes (‘\n’) instead of double quotes («\n»).

PHP will only evaluate escape sequences if the string is enclosed in double quotes. If you use ‘\n’, PHP will just take that as a literal string. If you use «\n», PHP will parse the string for variables and escape sequences and print a new line like you are expecting.

$text = 'text line one' . PHP_EOL . 'text line two'; echo ''; 

Will add each text on reparate line in texarea.

Читайте также:  Create array java inline

PHP Side: from Textarea string to PHP string

$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']); 

PHP Side: PHP string back to TextArea string:

$list = str_replace('|', '
', $r['db_field_name']); 

Источник

How do I start a new HTML line from PHP?

How do I cause the PHP to begin a new HTML line with each iteration of the loop (instead of printing everything on a single HTML line)?

5 Answers 5

You need to include newline characters in your output. Thus:

Note the double quotes, which are necessary for the escaped \n to appear as a newline character instead of a literal \n.

Another useful escape is \t, which inserts tabs into your output.

Lastly, as an unrelated tip, it is faster to pass multiple strings to echo with commas instead of periods, like so:

This is because using the dot causes PHP to allocate a new string buffer, copy all of the separate strings into that buffer, and then output this combined string; whereas the comma causes PHP to simply output the strings one after another.

since the new line character \n serves no point in html, you’re better not using it, keeps the files (fractionally) smaller

I agree. However, for debugging it is occasionally useful to put them in, and thus good to know how it is done.

The \n is a newline character if in double quotes.

Use the newline character in double quotes «\n» . Single will not work.

i find that using single quotes is easier than using .»\n» for example using

new line characters are echoed if they are within single quotes

You can insert newline characters that will appear in HTML source code by including literal carriage returns in your PHP strings, eg:

$count1 = '1'; $count2 = '2'; /* Note the opening single quote in the next line of code is closed on the following line, so the carriage return at the end of that line is part of the string */ echo ' 
This is row' , $count1 , 'and
this is row' , $count2 , ' 
';
This is row 1 and this is row 2 

as you’d expect, but the source code will presented as:

 
This is row1and
this is row2 

Trivial example maybe, but when you’re echoing a large string with several lines (like a table), it’s easier to debug the HTML source if these line breaks are present. Note that, if you indent with tabs or spaces within these strings, the HTML code will also have these indents. Of course, the browser will collapse all of them into single spaces, but it makes the HTML code easier to follow.

Источник

PHP — how to create a newline character?

I have tried many variations of the \r\n , but none work. Why isn’t the newline turning into a newline?

15 Answers 15

Only double quoted strings interpret the escape sequences \r and \n as ‘0x0D’ and ‘0x0A’ respectively, so you want:

Single quoted strings, on the other hand, only know the escape sequences \\ and \’ .

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string «\r\n» or using chr function chr(0x0D).chr(0x0A) ), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break some text after'; 

Make sure to check your editor for its line break settings if you require some specific character sequence ( \r\n for example).

@Pacerier The only way to have carriage return and new line characters in a single quoted string is entering the characters directly via keyboard (press Enter, depending on your editor’s settings on line endings). Otherwise just use string concatenation.

@Gumbo : You are saying Single quoted strings, on the other hand, only know the escape sequences \\ and \’. But I just printed a line with echo as ‘; echo ‘New line added’; ?> and seen the output in browser. It literally added new line between two strings I displayed. It means the character
or
gets expand in single quoted string. Are there any other such HTML characters apart from these three that can be understood by PHP inside the single quoted strings? I kindly request you to update your answer accordingly. Thank You.

@user2839497 What you are saying does not make any sense. There’s no browser context here. And of course the browser renders a newline when encountering a
. And of course there is nothing that prevents you from adding ‘
‘ in a single quoted string. But that does not render a newline in a simple text file.

@Pacerier For a single quote policy instead of linebreaking the string in the editor like Gumbo suggested in his comment you could use the other solution in Gumbos answer, example: ‘outputting a newline’.chr(0x0A).’in a single quoted string’.

Use the predefined PHP_EOL constant:

echo $clientid, ' ', $lastname, PHP_EOL; 

The constant value will be set according to the line endings of the operating system where PHP is executing. On Linux, it will be «\n» ; on Windows, it will be «\r\n» .

I could not get newline to work in the php sandbox I’m using so I tried PHP_EOL. Worked like a charm. Thanks. Guess it runs on Windows server.

This should be the accepted answer. I still don’t know why people keep using \r\n. I mean, I know Windows is the most common OS, but cmon guys, there ARE other OS’s out there

The browser will not parse linefeeds into two lines because a newline character in HTML means nothing. That’s why when needs a new line in HTML, one has to add some tag,
being the simplest one:

This will output the HTML newline.

Note that it’s a good idea to add a new line anyway, so you’ll have readable HTML when checking the page source.

With the way the question is asked it is likely that he is outputing this data to either a textbox or javascript, etc. for parsing or plain text reading not to be rendered as HTML

@PC3TJ when a million users come for the answer from Google, the way the initial question was asked becomes the least important thing in the universe.

Use the constant PHP_EOL to get the right character no matter the platform.

Strings between double quotes «» interpolate, meaning they convert escaped characters to printable characters.

Strings between single quotes » are literal, meaning they are treated exactly as the characters are typed in.

You can have both on the same line:

echo '$clientid $lastname ' . "\r\n"; echo "$clientid $lastname \r\n"; 
$clientid $lastname 1 John Doe 

Your explanation about the different types of string notations in PHP is fine as it shows the error made by the question author. But your code sample will not produce the expected result as the variables are used in a single quoted string and, as you mentioned, they will not be interpolated !

@Damien Flament, That’s what I said — the contents between single quotes are not interpolated, but the contents between the double quotes are. Perhaps my example is confusing because you are expecting to see the contents of $clientid and $lastname, rather than the literals «$clientid» and «$lastname». I think this was the heading of a table that I used during debugging. the following lines would have been echo «$clientid $lastname\r\n»

When writing «the expected result», I’m talking about the result the question author expected in its question. Adding the code shown on your comment to your answer and showing the result might improve your answer quality.

You also might wanna have a look at PHP EOL.

Actually \r\n is for the html side of the output. With those chars you can just create a newline in the html code to make it more readable:

echo "First line \r\n Second line"; 

that viewing the page will be:

If you really meant this you have just to fix the single quote with the «» quote:

Otherwise if you mean to split the text, in our sample ‘First line’ and ‘Second line’ you have to use the html code:
:

Also it would be more readable if you replace the entire script with:

echo "$clientid $lastname \r\n"; 

No, it shouldn’t. Because it’s mostly wrong. The first code block will render as First line Second line — any whitespace including newlines will compress to one space character in the rendered output. Also,
is anything but valid HTML.

Also keep in mind if you are sending out text emails with your php script, you will still need those line endings instead of breaks (
)

There is a handy PHP function for HTML output that adds a
tag to new lines:

echo nl2br("One line.\n Another line."); 

The question is not about breaking a line within a paragraph in HTML. It’s about the newline character !

For context, the nl2br() function converts newline characters to the
HTML tag. This might be useful for creating a newline on browser pages, but not a newline in text files, or JavaScript, or printing to the screen or console.

Nothing was working for me.

$NEWLINE_RE = '/(\r\n)|\r|\n/'; // take care of all possible newline-encodings in input file $var = preg_replace($NEWLINE_RE,'', $var); 

Works for me:

$admin_email_Body = $admin_mail_body .'
' ."\r\n"; $admin_email_Body .= 'This is line 2
' ."\r\n"; $admin_email_Body .= 'This is line 3
' ."\r\n";

There was literally a dozen answers suggesting the same. How come none worked for you? And what’s the point of the first code snippet? It doesn’t add, it removes the new line character.

For some reason, every single post asking about newline escapes in PHP fails to mention the case that simply inserting a newline into single-quoted strings will do exactly what you think:

Example 1 clearly does not print the desired result, however, while it is true you cannot escape a newline in single-quotes, you can have one:

Example 2 has exactly the desired behavior. Unfortunately the newline that is inserted is operating system dependent. This usually isn’t a problem, as web browsers/servers will correctly interpret the newline whether it is \r, \r\n, or \n.

Obviously this solution is not ideal if you plan to distribute the file through other means then a web browser and to multiple operating systems. In that case you should see one of the other answers.

note: using a feature rich text editor you should be able to insert a newline as a binary character(s) that represents a newline on a different operating system than the one editing the file. If all else fails, simply using a hex editor to insert the binary ascii character would do.

Источник

PHP, how to print new line strings

BTW this output comes like this in the email, bc I am sending all this data in email, I tried adding \n and
but didnt worked :S Any suggestions?

3 Answers 3

«\n» will work. You have to use double quotes rather than single quotes. If you want new lines in html, you need to echo
, which you can do in either single or double quotes.

I suggest better using PHP_EOL constant rather than «\n» because \n doesn’t work in most Windows servers. PHP_EOL always returns the correct line break and avoids a lot of headache if you ever moved to a Windows server.

$message='Name and Surname:'.$name.' '.'
Birthday: '.$Birthdate.' '.'
Nationality: '.$Nationality;

@AdnanShammout Looks cooler for sure, but why the extra keystroke? lol Precious nanoseconds . Actually, 2 extra keystrokes and is only required if using XHTML .

Those two key strokes make you XHTML compliant. Which makes you cool, and renders faster in the browser. (I made the last part up.)

To create an actual new line, use «\n» in double quotes. (Or use PHP_EOL).

To create a new line in HTML, use «
«. (This is not symantic HTML, many would argue to use tags.

$message = 'Name and Surname: ' . $name . "\n
" . 'Birthday: ' . $Birthdate . "\n
" . 'Nationality: ' . $Nationality . "\n
";

You could also simplify the code by using:

$message="Name and Surname: $name
\nBirthday: $Birthday
\n Nationality: $Nationality
\n"

Источник

Оцените статью