I have a problem with the following:
I want to make a page that gets a file (I upload it), reads it and outputs it in an html file.
I am uploading the file and saving the contents in a mysql DB just fine, but when I show it again, I don't have any <br />'s there (maybe because the file should have \t\n or something.
How can I make it show it like it was originally written. (In the DB I see it with the fine spacing).
You probably want nl2br(). It will transform all line breaks to <br>s
You can either wrap inside <pre></pre> tags to display it as it is, or better yet use nl2br() function to add html break lines <br /> before any newline/carriage return /r /r/n /n
Are you sure the problem isn't just in the HTML? Multiple whitespaces convert to one in web browsers. In modern browsers, you can use the CSS white-space property to prevent that.
body { white-space: pre; }
Alternatively, you could wrap that section of HTML in a <pre> element, or you could hardcode extra spaces into
at time u store file data in database encode data using htmlentities() and at time of displaying decode it using html_entity_decode()
Related
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page
I am looking to insert a user's input from a textarea into a mysql database. As of now, if the user types the following:
"Hello.
hello."
It will be inserted into the database without the line breaks. How can I fix this?
You are probably using phpMyAdmin to tell that the line breaks aren't there. If that's the case, then it is lying to you. phpMyAdmin displays the contents as HTML, and line breaks are transformed to a single white space.
Even if you're just outputting the value and not using phpMyAdmin, your HTML will still collapse the white space. To preserve the format, output it between <pre> tags or in a span with css style white-space: pre
Actually the line breaks are inserted in the database, you just can't see them, because they are "\r\n", this are Line Feed and Carriage Return, read more about them here
I believe that you should store the data like this: no conversion made, when you write the content to your HTML, you simply have to do an nl2br($content).
And it's also easy to edit, because when outputting that content to an textarea tag, it automatically recognizes the "\r\n"
If using PHP, i suggest using $message = nl2br($message);
This will turn line breaks into br's for you.
I'm doing a web builder and I'm having a bit of trouble.
I want to have a textarea that you can enter text that you want to your TEXT element. This element I disabled the HTML when it´s later off previewed by simply putting a .innerText before posting preview. Though, I might just need the <br> tag.
I can use PHP or JS. Any ideas?
Use nl2br(htmlspecialchars($str)) when displaying the text. Note that the order of the function calls matters - first you escape all HTML in the string and then you convert linebreaks to HTML linebreaks.
When you are typing in a textarea, and the return key is pressed,
What actually goes on behind the scenes is this
Hi, \n There
which produces the following in the textarea.
Hi
There
Hence, what you would need to do is essentially change the \n (newline) to break tags.
http://php.net/manual/en/function.nl2br.php
Just use a plain textarea, afterword use a function like nl2br when you display it in your html page
nl2br(htmlspecialchars($string))
changes the line breaks to <br /> tags, so you can display them in html as seen in the textarea.
Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page
Let's say I have the following text file:
This is the first line of the text file.
This is the second line,
and here goes the third.
When using
echo file_get_contents($_SERVER{'DOCUMENT_ROOT'} . "/file.txt");
The output is
This is the first line of the text
file. This is the second line, and
here goes the third.
How do I prevent the layout from changing?
Thanks in advance :-)
In HTML, new line characters (\n or \r\n) don't cause actual line breaks to appear in the rendered page. Here are two possible solutions:
Use the nl2br() function to convert newlines to <BR> tags. This will work for some layouts, but not for ASCII art or others that rely on multiple spaces (which are reduced to one in HTML).
echo nl2br(file_get_contents(...));
Wrap the result in <pre> tags. This will keep all layout, but can look a bit ugly. You can style <pre> tags with CSS to make them prettier, if you'd prefer.
echo '<pre>' . file_get_contents(...) . '</pre>';
Every time you are gonna use PHP, it must be done in 2 parts:
Write your page in pure HTML. Make it work as desired. Ask HTML questions if any.
Write a PHP script which produce the the same text.
An HTML tag that could help you is <pre>
Another way to tell to browser that here hoes plain text is to send appropriate HTTP header
So, it this text is the only text being displayed on this page,
header("Content-type: text/plain");
readfile($_SERVER['DOCUMENT_ROOT'] . "/file.txt");