PHP doesn't show line break in textarea value in echo - php

I am creating a post_blog.php file where user will enter all fields one by one like post_title, post_author etc.
When user write post_text which contains many paragraphs and i get that text by using post method in php it displays me all value in plain text . it do not contain any paragraphs etc.
Here is the code :
<textarea rows="400" cols="100" name="post_text">
Enter post text here .. upto 5000 characters .
</textarea>
in php :
if()....
echo $post_text = $_POST['post_text'];
Demo Input in post_text:
Enter post text here
.. upto
5000
characters .
OUTPUT:
Enter post text here .. upto 5000 characters .
Expected OUTPUT :
Enter post text here
.. upto
5000
characters .

Your problem isn't in the php; Your problem is that HTML doesn't accept white spaces (line breaks, sapces, etc.) and display them as one space. There are some solutions:
Use the <pre> tag, which shows the text as-is:
<?php
...
echo '<pre>' . $post_text = $_POST['post_text'] . '</pre>';
Use the CSS white-space: pre; definition, which is excatly like the <pre> tag, except that it's can be applied to any element, and you cannot style a <pre> tag:
<?php
...
echo '<div style="white-space: pre;">' . $post_text = $_POST['post_text'] . '</div>';
See MDN Docs about CSS' white-space declaration.
The last solution is to replace any line break with the <br> tag and any space with :
<?php
...
echo $post_text = str_replace(' ', ' ', str_replace('\n', '<br>', $_POST['post_text']));

When user press Enter, line break \n inserted in string but in html doesn't show. You should convert it to <br> to showing in html. The str_replace() can replace it in string.
$newStr = str_replace("\n", "<br>", $str)
Also you can use native function nl2br() that inserts HTML line breaks before all newlines in string as #VictorFedorenko mentioned in comment
$newStr = nl2br($str)

Related

add newlines or line breaks after p closing tag with php

I have this text in a MySql database:
First paragraph very long.
Second paragraph very long.
Third paragraph.
I add p tags and it works:
$text = preg_replace("/\n/","<p>",$text);
$text = '<p>'.$text;
I try to add line breaks when I echo to a html page. I tried 3 different things. But none of them seem to work:
$text = preg_replace("/<\/p>/","</p>\n\n",$text);
$text = preg_replace("/<\/p>/","</p><br><br>",$text);
$text = nl2br($text);
echo $text;
If I go to the web inspector in the Safari browser, I get this:
<p>First paragraph very long.</p><p>Second paragraph very long.</p><p>Third paragraph.</p>
I would like to have this:
<p>First paragraph very long.</p>\n>\n
<p>Second paragraph very long.</p>\n>\n
<p>Third paragraph.</p>\n>\n
It seems that my regex does not select <\/p> even when I escape it. I do not understand. What is wrong?
Presuming you need newline control chars (and not html line break tags):
$text = "First paragraph very long.\nSecond paragraph very long.\nThird paragraph.";
$text = '<p>' . preg_replace("~\n~", "<p>\n\n</p>", trim($text)) . '</p>;
Note trim is used incase you have leading or trailing newlines, ~ is used as a delimiter, because / is a poor choice when dealing with html, causeing you to escape all over the place.
It is not apparent in the above example, but using some of your reqex as an example:
preg_replace("~</p>~","</p>\n\n",$text);
is much easier to read than:
preg_replace("/<\/p>/","</p>\n\n",$text);
Also, you dont need regex, you could just use str_replace:
$text = '<p>' .str_replace("\n", "<p>\n\n</p>", trim($text)) . '</p>;
Or even explode/implode:
$text = '<p>' . implode("</p>\n\n<p>", explode("\n", trim($text))) . '</p>';
If it was html line breaks you wanted, then you could just edit the replacement argument to:
"</p><br><br><p>"
in any of the above, but it would probably be better to use some css:
p{
margin-bottom:10px;
}
You don't need regex, simple str_replace works (in your example):
$text = str_replace( "</p><p>","</p>\n<p>",$text );

substr doesn't work after using strip_tags

I want to remove all tags before showing them on preview mode (just some text).
I have this code:
$text = strip_tags($item['content']);
echo substr($text,0,13);
here is my $item['content'] is something like this
<div class="note note-success">
<p>
Font Awesome gives you scalable
vector icons that can instantly be customized — size, color, drop
shadow, and anything that can be done with the power of CSS. The
complete set of 439 icons in Font Awesome 4.1.0
</p>
For more info check out: <a target="_blank" href="http://fortawesome.github.io/Font-Awesome/icons/">http://fortawesome.github.io/Font-Awesome/icons/</a>
</div>
The problem is that when I use substr it doesn't show anything, but when I use normal echo, it shows the content of the variable that was stripped before.
Does strip_tags not give string output?
Try to remove whitespaces before outputting your substring:
$new = str_replace(' ','',$text); (Use trim instead as #mario.klump said)
$text = strip_tags($item['content']);
$new = trim($text);
echo substr($new,0,13);
strip_tags() function works only when following type of html text. what you are doing is convert html encoded text so, it will not be parse.
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
For your example you can use like this:
$text = htmlentities($item['content']);
echo substr(html_entity_decode($text),0,13); or
echo substr($text,0,13);

text-indent on user formatted text

I would like to use the text-indent property (or something like this) to add a indentation of the first line of each paragraph.
First the user can write his text in a textarea, then save it in a DB.
When I want to display this text i use :
$exhib = $res->fetch_array();
echo "<div class='infoContent'>". nl2br($exhib['description']) . "</p></div>";
The line return of the user are stored as \n in DB, and modified to <br /> by nl2br. With my CSS :
.infoContent
{
text-indent: 10px;
}
only the first line is indented. (normal behavior).
Q : How can I make this indentation automatic for each line after a <br /> tag ?
I tried a ugly solution, but it doesn't work because empty paragraph section <p></p> doesn't create another line return (in case the user enter 2 line return \n\n).
echo "<div class='infoContent'><p>" . str_replace("<br />", "</p><p>", nl2br($exhib['description'])) . "</p></div>";
I can replace <p></p> tag by <br /> but it seems to be a very bad solution...
EDIT:
JSfiddle
Thanks
\n\n usually means a new paragraph (enter). The white space between paragraphs is CSS and is actually default browser styling (1em I think?). \n is a <br> (shift + enter).
So don't use nl2br() and do it yourself:
$text = '<p>' . htmlspecialchars($text) . '</p>'; // HTML ENCODE!
$text = preg_replace('#\n\n\n*#', '</p><p>', $text); // 2 or more \n
$text = preg_replace('#\n#', '<br />', $text); // all left-over \n
$text = preg_replace('#><#', ">\n<", $text); // if you like </p>\n<p> with a newline between, like I do
http://3v4l.org/b0AhL
This is pretty much what Markdown does (and Textile and those): 1 newline = BR (not exactly in Markdown) and 2 newlines = P. I always use simple Markdown for rendering plain text.
When you submit your textarea, instead of using CSS to indent only the first line, you can use (non-breaking space).
when you submit your text area, I assume you grab it as such:
$userText = $_POST['description']
Well, before you submit to your database, you could use a simple replace - After you grab the text:
$userText = str_replace("\n", "\n ", $userText);
Then submit that to the database. When it comes back, the nl2br will still make the \n into a <br /> and then it won't see the , though the HTML will see them as four spaces (equal to an indent).
It's dirty, but simple!
Reference: http://www.w3schools.com/php/func_string_str_replace.asp

how to print special characters which are a string in variable with php?

I'm making this chat server, but it doesn't work quite well. When you send a piece of text, it first gets encoded by the function base64_encode() and then gets sent to a MySQL database.
Then the receiver gets the text from that same MySQL database, which is of course first decoded by the function base64_decode().
The only problem is with the special characters like \n \' and \t: when I get the data from the database and print it between two textarea tags, I see \n as a string, and not as actual line breaks.
In short, I need to fix this problem:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
//The result I want
//<textarea> Line 1
//Line 2 </textarea>
The function nl2br doesn't work, because tags inside a textarea tag won't work, and also because there other characters like apostrophes.
Could anybody help me?
Thanks!
You need to enclose your string into double quotes, for special characters to be evaluated.
$String = "Line 1 \n Line 2";
print '<textarea>' . $String . '</textarea>';
If you change this:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
to this:
$String = "Line 1 \n Line 2"; // double quote
print '<textarea>' . $String . '</textarea>';
... you will get the output you want.
This one is also works same as using " ... ", however maybe helps in your case:
$string = <<<EOT
Line 1 \n Line 2
EOT;
echo '<textarea>' . $string . '</textarea>';
As the others said, your problem is Single-Quotes.

delete the <br /> from the textarea?

I am trying to do a line break after the message "Original message", I have tried with this but It keeps showing me the
---Original message---<br />
message
<textarea id="txtMessage" rows="10" cols="50"><?php echo nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']));?></textarea>
I want something likes this:
---Original message---
message
any advise?
This should do what you want it to:
<?php echo str_replace('<br />', " ","---Original message---\n".$array['message']);?>
nl2br — Inserts HTML line breaks before all newlines in a string (from php.net)
Example:
echo "<textarea>HI! \nThis is some String, \nit works fine</textarea>";
Result:
But if you try this:
echo nl2br("<textarea>HI! \nThis is some String, \nit works fine</textarea>");
you will get this:
Therefore you should not use nl2br before saving it to database, otherwise you have to get rid of <br /> every time you try to edit text! Just use it when you print it out as text.
echo nl2br(str_replace('<br/>', " ", ... ));
should be
echo str_replace('<br />', ' ', ... );
The php function "nl2br" takes newlines, and converts them into br tags. If you don't want that, you should probably remove it :).
Heh, beaten by Ryan.
You're trying to replace <br/>, but the original text has <br /> (note the space).
You are removing the HTML breaks, then adding them back! Look at your code:
nl2br(str_replace('<br/>', " ","---Original message---\n".$array['message']))
First, str_replace replaces '<br/>' with a space. Then, nl2br adds a <br> for every newline (\n) it finds.
Remove nl2br call and it's done.
If you want to do nl2br on all of the text except for what's inside the textarea you could do this:
function clean_textarea_of_br($data) {
return str_replace(array("<br>", "<br/>", "<br />"), "", $data[0]);
}
$message = preg_replace_callback('#<textarea[^>]*>(.*?)</textarea>#is',clean_textarea_of_br,$message);

Categories