Displaying a Paragraph with line breaks - php

I have a situation where users can enter a question and this is written into a mysql table.
I then show this text in an admin panel for reivew and reply.
I can see the text in the admin panel has the same new lines (chrome developer) but it shows as one block of text (no new lines)...
I'm currently using to show this text... I've tried a but it still hows without line breaks.
How should I go about displaying this text with the new lines it contains? Is there a HTML tag to do this or do I need to use PHP and sub \n for br tag?
example below:
<span class="description">txthere
txthere
txthere
txthere
txthere.</span>

On display you can use CSS to style the HTML with the original line breaks, try white-space:
white-space: pre-line;

if it is textarea or console basically it puts a line break using /n which is not recognized by the browser as line break, you need to convert /n to <br/> to tell the browser that this is a line break, use this php's function
nl2br();
more information here: http://in.php.net/nl2br
this will convert all your /n tag to <br/> tag so that browser process it as line breaks

Related

PHP strip_tags stripping new line in ckeditor text area. How to stop it?

I'm using below line to remove text formatting from ckeditor result text area for some reasons.
html_entity_decode(strip_tags($result_string_from_ckeditor),ENT_QUOTES,'utf8');
It works great except than it stripping new lines and all separate lines will be join to each other.
I tried solution in this post and used below line, but no success
html_entity_decode(strip_tags(nl2br($result_string_from_ckeditor),'<br>'),ENT_QUOTES,'utf8');
Edit: for example
ckeditor source text:
<p>This is an example</p>
<p>of problem in strip_tags</p>
Source result after using command:
<p>This is an exampleof problem in strip_tags</p>
Many thanks.

Line break in MySql/php

I've a form where I create different texts. To use line breaks correctly I'm sending the input through nl2br. It works perfectly, using this code:
$formSubject = nl2br(htmlentities( $_POST["formSubject"], ENT_QUOTES, 'UTF-8'));
The problem I'm having is that I also have an Edit function and when I open a text to edit I get the line breaks but also <br/> in the text (which I don't want because the users of this will not be familiar with html) and if I save (even without changing anything) it will add another line break (which means double <br/><br/> if I open it to edit again. Is there any solution to this?
you can remove <br/> (str_replace()) or all html tags (strip_tags()) before edits
or
you can use WYSYWIG Editor (like tinymce or ckeditor) to edit text with html tags

What is the best way to parse text and code in my PHP blog?

Usually, I use nl2br() and it does come out just like it's entered in the textarea, but this causes a problem when using bbcode or posting code in <code> or <pre> tags, since it adds extra line breaks.
For example this code
[sub-title]test[/sub-title]
some text here.
I'd like it to look like that when displayed in the browser, but because [sub-title] becomes <div class="sub-title"> the <br /> adds an extra line break, so it will look like this (with 2 line breaks in between)
**test**
some text here.
I haven't fully looked into it yet, but could the PHP bbcode parser help, or is the only/best solution to use regex?
You can use nl2br()
Example
$message = nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message)));

insert user input into mysql database as styled by the user (with line breaks)

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.

No newlines in php echo from mysql

I have a news section on my website that has inline edit via ajax. I write some text, it gets stored and it get's displayed.
But for some reason there are no newlines or line breaks as they call them. I tried numerous stuff. And the only way a newline is displayed is if i press 'Enter' manually. Otherwise neither wordwrap nor nl2br help.
echo ' <span class="subheader"> <strong>'.$array["title"].
'</strong> </span> <br /> '.
'<div style="margin: 0px 0px 0px 10px; width="20px"">'.
' <div id="quickDeleteNewsItem"> '.
'<div style="width:20px;" id="post_message_'.
$array['id'].'" style="display: inline;">'.
$array["content"].'</div> </div> </div> ';
In order to answer this, we'll need to see some code.
What code are you echoing when you expect a line break?
With the information posted now, all I can say is: If you aren't seeing line breaks when viewing text in a <div>/<p>, you need to either use line breaks (<br />) or put each paragraph of text in your news section in a block-level element, like a <p> tag.
try to encode content with encodeURIcomponent when posting it via ajax.
content = encodeURIComponent(content);
I think we need to see more of your code. I can think of at least three places the data is held:
In a on your page for editing
In the on your page for viewing
In the database
and two types of line break:
Hard breaks from when you have pressed enter (applies to all three data stores)
Soft breaks, where the line is longer than they box containing it (applies to textarea and div, maybe between different words).
It sounds to me that you are seeing soft breaks in your textarea, but not when the text is moved to the div using javascript (or using php and a database?). If so, then it's almost certainly that the containing div is too wide - use firebug (or similar) to inspect the div and make sure it is being set at the correct width.
replace the line brakes with html brakes
str_replace("\n","<br />\n",$data)

Categories