Displaying html stored in a database with the appropriate line breaks - php

I have code written in a database in several languages, html, css, php, etc.
I want it to show on my page with the appropriate line breaks. Like this:
https://cdn.discordapp.com/attachments/668127780927701032/1016459728194375780/unknown.png
So, when I display html, it executes it instead of displaying it, which is normal.
So I use htmlspecialchar, except that I can't make a line break anymore, everything is displayed on one line. And I can't br out of this.
How do I do that?

Apparently your string contains literal \n characters, not newlines. So you need to replace that with <br>.
echo str_replace('\n', '<br>', htmlspecialchars($stmt['html']));

Related

Why do I get \n instead of a new line getting data from AJAX?

I have a problem: so I have a data thats coming out of the database and stored in a text area. When the user changes the data in the text area, the content is sent to javascript and via AJAX (POST to a PHP script) the database is updated. This works fine until the user starts adding newlines. Then javascript transforms this into a \n-character and thus it gets stored in the database as \n.
What I want is to have actual newlines in my database and not the \n newline-characters. Is there any way that I can use php to replace the \n with an actual newline (NOT a br)? I have tried altering the database field after the edit with the char(10), but for some reason this is not working in the script except when I do it manually in phpmyadmin?
When editting with a full php request, the newline in a text area is correctly stored as a char(10) in mysql, not as \n.
Anyone got a clue?
Store it as it is but escape first with real_escape_string
real_escape_string converts what is a newline into the 4 character string '\n\r'
$text = $mysqli->real_escape_string($text);
Use [nl2br][1] function to replace /n with newline
Insert line breaks where newlines (\n) occur in the string:
<?php
echo nl2br("One line.\nAnother line.");
?>
The browser output of the code above will be:
One line.
Another line.

nl2br() works on one page but the exact same code doesnt work on another

Ok so I simply call from mysql some text, and then I run it through nl2br to preserve linebreaks and all its symbols and what not.
code looks like this on both pages - <? echo nl2br($row[text]); ?>
now on one page it preseves line breaks quotations hyphens, everything. and on the other preseves line breaks, but has symbols within it like instaed of hyphons or quotations.
For example - Jenny and Jonny “Big Wave†video
Any help would be great
I think you may be hitting an issue with HTML entities:
<? echo nl2br(htmlentities($row[text])); ?>
This probably won't resolve the issue as you probably have a character encoding issue too. Make sure your database field is storing data as UTF-8, then add the following line immediately after your mysql_select_db() line:
mysql_set_charset('utf8');
Once that's done, outputting the text should work something like this:
<? echo nl2br(htmlentities($row[text],ENT_COMPAT,'UTF-8')); ?>
An easier fix might be to ensure the "funny" quotations in the DB are replaced with "normal" quotations - “ and ” versus " (you'll hit the same with single quotes/apostrophes and em dashes/en dashes/etc copied from Word).

How do i enable line breaks in php?

I am making a comments system in which i can accept user input with line breaks.
I don't want to show the \n or \r thing
Please help me with this
nl2br($string);
is fast and easy
They are enabled by default. If you are outputting the text to a web browser, make sure to use nl2br or the white-space attribute in CSS.
using preg_replace
simply replace it
preg_replace('/\n/'," ",$str);
\n should do the trick.
if you are trying to output a textarea, then use nl2br();
also:-
If you are trying to format your HTML source, you should use the constant PHP_EOL. The main reason being that on windows machines the EOL is \r\n and on UNIX machines it is \n. With a properly installed LAMP set up just use PHP_EOL like so.
$html.="<p>This is my HTML</p>" . PHP_EOL;
Line breaks will be stored just like any other character.
\n is an escape code that allows you to explicitly insert a line break into a string, but I don't think that it's relevant here.
The issue you're actually facing is that HTML does not impart any visual meaning to a line break. Line breaks within HTML code do not, under normal circumstances, equate to a line break on the screen.
One way to render a line break in HTML is to use a line break tag, or <br>.
In PHP, you can automatically convert line breaks to <br> with the nl2br function. Applying this to your comment text when you output it into HTML will enable you and other visitors to see the line break visually.

PHP: How to prevent unwanted line breaks

I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.

PHP form removing line breaks

I use a php form processor script that works fine. Except when users submit text in a multi-line text field, any line breaks or new lines are stripped out of the resulting string variable that is passed on. This often makes it unreadable by whoever receives the form results.
I'm no php expert but am sure the answer lies in the code that is stripping characters. What I'm unsure of is if I stop it stripping characters will this result in a security risk?
The strip array reads:
array('*', '|', '>', '<', '/', '\\\', '\"', 'Bcc', 'BCC', 'bcc');
What can I change here to retain the line breaks?
Thanks in advance for any help.
If your problem is with the submitted string then this means that the submitted string did not contain any line breaks or newline chars.
In one occassion i looked up the wrap="(hard|physical)" attribute on the text area. Some values of this attribute force the textarea to maintain line-breaks in the user text.
Did you try using nl2br($text) on the submitted text;
I believe you have an issue at the rendering phase. Have you tried:
echo nl2br($text);
Where $text is the text you're talking about.
Nothing there is stripping line breaks.
It's more likely that you're not doing anything on the display side to make the line breaks display. You're outputting HTML, and to HTML a line break is just more whitespace. You'll probably benefit from applying nl2br().

Categories