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 wordwr开发者_JAVA百科ap 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 echo
ing 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)
精彩评论