nl2br doesn't work with htmlspecialchars - php

So i'm trying to get my nl2br to work but it just wont work the way i hope it would.
As it retrieves text from a database, i want it to recognise the \n with the nl2br I have htmlspecialchars which is the reason it doesn't work. How would i solve this issue?
My code:
The post variable looks like this
$post = nl2br($row['p_post']);
Which means that it ends up inside the htmlspecialchars()
echo '<p>'.htmlspecialchars($post, ENT_QUOTES).'</p>;
I tried to put it like this echo '<p>'.nl2br(htmlspecialchars($post, ENT_QUOTES)).'</p>; but that ended up looking as if there was no nl2br at all.
How it looks from the start
How it looks after i've added the nl2br

You're double encoding the tags:
$foo = "how\ndoes\nthis\nlook";
$post = nl2br($foo); // $post is now "how<br>does<br>this<br>look"
$temp = nl2br(htmlspecialchars($post)); // $temp is now "how<br>does etc..."
echo '<p>how<br>does< etc...';
You converted the line breaks to break tags, then you html encoded the break tags to html character entities, so your browser renders those tags as "visual" tags, and not HTML line breaks at all.

This was fixed, it seemed that another variable collided with it somehow so that it wouldn't actually make the <br> tags.

Related

PHP output string and maintain spacing [duplicate]

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

remove <br> tag on ckeditor output

I have integraded a textarea box with ckeditor and each time I press enter on the backend side for a new line it outputs <br> on the front end, is there a way to remove <br> on the front end as I don't want the html tag output on the front end
I line of code looks like the following
echo "<strong>Sites Linked Out To</strong>: " . $row->sites_linked_out_to;
is there a way to remove the html <br> tag before it gets added to the database or after?
Thank you in advance
php strip tags function write in tags secondary parameter, keeping tags, other html tags clean
strip_tags($input, '<a><img><div><strong>');
more information for strip tags function http://php.net/manual/tr/function.strip-tags.php
You could call nl2br() when you insert the content.
This will replace any <br> with a new line \n
Note though; If you are actually seeing the tag its probably being url encoded somewhere. If you call nl2br() before this encoding takes place it should work.
You could also strip other undesired tags using strip_tags. Do this after replacing the new line.
define('ALLOWED_TAGS', '<p>,<strong>,<ul>,<li>,<ol>,<em>');
$sContent = strip_tags( nl2br($sContent), ALLOWED_TAGS);
Note though, this wont strip out <a href='#' onclick='DO_SOMETHING_BAD'>click me</a>
You could look at using a library such as html purifier to sanitise input. Or just ensure you sanitise all output correctly.
See nl2br() and strip_tags for more info.
I know I am late but this may help someone..
Use
htmlspecialchars_decode($your_string);

Passing newlines using PHP to a textarea

I'm trying to make a list of email addresses (each one on a new line). I pull the array from my database, explode it along the comma delimiter, and run it through a foreach loop.
$emailsList = "";
foreach($emails as $email)
{
$emailsList = $emailsList . "\n" . $email;
}
echo "Additional Report Emails<textarea name='showReportsEmails'>".$emailsList."</textarea>";
When I look in the textarea itself, it will literally show the line break tag in the textarea. What can I do to get rid of this and have the text behave as I want?
<br />
email1#gmail.com<br />
email2#gmail.com
Thanks!
It looks like when the result is printed in the textarea, the PHP new lines (\n) are converted to <br/>s. So, maybe there's a nl2br() function being used to output the result into the textarea. It should be fixed by removing that. Otherwise, you can use the code below when setting the textarea's content:
<textarea><?php echo implode("\n", array_map('trim', explode("\n", strip_tags($emailsList)))); ?></textarea>
The code above first removes all html tags from the $emailsList variable, and then also removes white spaces before and after lines, so you would be good to go with this.
You should be using Carriage returns instead of HTML code in the textarea.
\r
The name suggest the the element can only hold text. It doesn't parse as HTML rather it does so as text.
I tested this code:
<?php
$value = "Hello,\nname\nis\nScript47.";
echo "<textarea>$value</textarea>";
?>
Output
Edit 1:
You could also use \r to see if it helps.

PHP: Character escaping not working with double quotes unless there is a <pre> tag?

Please can someone tell me where I'm going wrong?
//outputs : Hi, my name is Jackal I like PHP
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>
Whereas if I use pre tag
//outputs: Hi, my name is Jackal
// I like PHP
<pre>
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>
Can someone please explain why character escaping isn't working?
thanks
PHP is interpreting that line break. Look in the source code of the webpage. You'll see that you have 2 lines in the source.
However, HTML handles line breaks differently. To perform a line break in HTML, use the <br> tag. This will make the HTML output over 2 lines on the webpage itself. However, in the source code, it will still appear as a single line (unless you add that line break).
What <pre> is doing is telling the HTML engine to output the exact string as-is since the text is preformatted, including interpreting that line break. Also note, you should add a closing </pre> tag at the end of the block that is preformatted.
If you have this in your HTML:
<p>Hello,
World!</p>
Does it appear on one line, or two?
Usually, the answer is one: whitespace is condensed into a single space.
<pre>, however, has the default CSS of white-space:pre, which does NOT condense whitespace.
You should echo "<br />", or apply white-space:pre (or, even better, white-space:pre-wrap) to the container element.
Because in your browser \n shows just a whitespace and does not appear as line break unless you use pre-formatted HTML tag <pre>.
e.g. following will display with a line break in browser:
echo "Hi, my name is Jackal <br />I like PHP";
or this one also:
echo "<pre>Hi, my name is Jackal \nI like PHP</pre>";
As you can make out that <pre> tag is for displaying pre-formatted text hence \n does show as line break.
Character escaping is working. Look at the source code, there you'll find the new line. In order to make it visible in a browser you need an html tag (<br />) or the wrapping <pre>
<br>
is HTML.
<br />
is XHTML (recommended).
\n
is a newline in the code (works while writing to file, but doesn't output to the screen).
alternately you can use nl2br as below
echo nl2br("Hi, my name is Jackal \nI like PHP");
The newline character in php actually echos a new line itself.
However, the output of php's echo statement is still the source code of the web browser. Hence, the browser condenses all the extra line breaks and spaces. For example:
<p>Hello
there</p>
outputs
Hello there
despite the white space. To get around this issue, use the <br>/<br/> tag to tell the browser to make a newline, instead of tell php to make a newline.

Why does PHP echo'd text lose its formatting?

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

Categories