No newlines in php echo from mysql - php

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)

Related

Unwanted p and br tags in ckeditor

I am using ckeditor for writing articles for my website. The problem is when I add a a space within an article i get something like the following:
<p></p><br />
Can anyone help me with how to configure ckeditor such that when I press enter with the attempt to add a paragraph, I actually get a paragraph and and not the line i posted on top. As a result I am getting one paragraph and I have to go o the database to put the br tag inside the p tags for me to actually get a space to separate the paragraphs.
I press enter with the attempt to add a paragraph, I actually get a paragraph and and not the line i posted on top.
But by default you actually get a paragraph. For example if you press enter in this situation (^ represents selection):
<p>foo^</p>
You will get:
<p>foo</p>
<p>^<br /></p>
The <br> inside the empty paragraph is called a bogus <br> or filler <br> and it must be there, otherwise the block will be 0px high.
So I truly don't understand your question and I advise you to check your configuration, because by default CKEditor produces paragraphs.
PS. Setting config.enterMode to CKEDITOR.ENTER_BR is not recommended. Editor works best with ENTER_P which is the default setting. If you don't like spacing between paragraphs just edit the contents.css file and add a rule there which removes margins around block elements like p, h1, etc. Remember to clear cache after doing so. Read more here: Enter Key Configuration.
You need to press Shift + Enter in order to get just a <br> tag.
This functionality is also 100% configurable.
Demo

text box submits empty html tags to sql database

This one is kind of complicated to explain but here we go
I have a text box sent up as an iframe so I can allow people to make their text bold or italic before submitting it to my database. I'm working in php and sql.
I've just discovered that if you were to enter a bunch of blank lines I get a bunch of
<br><br><br>
etc...' stored in the database.
I already have functions in place to strip out all unwanted html apart from paragraph and linebreak tags, and of course bold and italic, but what I now need is a function to check if the content is entirely html tags and no actual text inbetween them.
I've no idea how to go about this.
I'd like to allow something like '
<br> I am <br><br><br>
but not
<br> <br> <br>
or
<br> <br><br><br>
or something similar, empty tags or tags with just white space. How would I go about this?
I think I'm pretty clear on my problem without pasting any actual code as such, but I'm but I'm happy to edit this if you want :)
Cheers
Just remove all html tags and trim the remaining string. If nothing is left, there is no content:
if (empty(trim(strip_tags($your_string))))
{
// no content
}

Displaying a Paragraph with line breaks

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

PHP code line break `\n` causing gap between elements

I'm echoing a series of HTML elements using PHP. I'm using \n to cause code line breaks to make the source code more organized and legible.
For some reason, the use of \n in a specific location is causing a mysterious gap between the HTML elements. In firebug, this gap is not showing up as a margin, or padding, but rather just a gap.
Here is the PHP in question:
Note: As you can see, I have removed all of the PHP inside the tags as I'm pretty sure it is not relevant to this problem.
echo '<ul ... >'."\n";
while($row = mysql_fetch_assoc($result_pag_data)) {
echo '<li><a ... >'."\n".
'<img ... >'."\n".
'</a></li>'."\n"; <---- THIS IS THE \n THAT SEEMS TO BE CAUSING THE GAP
}
echo '</ul>'."\n";
Have you ever seen anything like this before, a presentation gap associated with PHP line breaks?
If so, what is the reason for it?
Is it really that important that I use \n in my code?
That's normal. A \n line break has no meaning in HTML, so it's interpreted as a space character. If you don't want that gap, then eliminate the \n, or rewrite the html so it's not relevant:
<li><a ...><img ...></a></li>
As a general rule, tags which can contain text should never have their closing tags on a line by themselves, for this very reason.
Following up on your 'where to put \n' question. This comes down to personal preference, but I tend to format my html like this:
<table>
<tr>
<td><a href="some big long ugly url">
<img ....></a></td>
</tr>
Since <tr> can't contain any text on its own (in valid html), it's ok to put on its own line. But the </a> and </td> are both tags that CAN contain text, so I put them right up against the end of the 'text' (the img tag in this case), so that the Phantom Linebreak Menance (coming soon to a starwars ripoff near you) can't strike.
Note, of course, that my example does have a line break and indentation between the opening <a> and the <img> tag, so that's another place where a "must be right next to each" other layout would cause a gap. If you need a series of things lined up smack dab against each other, than you basically can't use line breaks anywhere in that section of the page.
The whitespace is translated into (empty) HTML text nodes, which take up some space (you can test this by walking the DOM). There is no solution to make these disappear that I know of other than removing the whitespace from your HTML in the first place.
Of course it's not only \n that would cause this behavior; spaces or tabs would do exactly the same as well.
In that particular case the newlines are used to prettify the html source, keep it readable via view-source. That's quite common actually. (Yet redundant.)
As said by the other answers, it does not have meaning normally. Albeit this can be overriden via CSS and the attribute (which we can assume is not the case here):
white-space: pre-line;
You should only output a newline where you in fact want a newline in the output. In HTML, a newline is whitespace, just like the space character.

TinyMCE - stripping tags and then adding tags - weird behavior

I have a tinyMCE textarea on my page. I'm trying to create a "quote and reply" function for a forum program and this portion looks like this:
if ($special == 'quote') {
$dataContent['message'] = strip_tags($dataContent['message'], '<p>');
echo '<em>'.$post->authorName.' said:<br />'.$dataContent['message'].'</em><br /><br /><br />';
}
echo '</textarea>';
Alright, simple. So, it should strip all tags except the line formatting (<p> tags) and then add an <em> tag to the entire thing to italicize it, I have em set up in the styling to always be font-style: italic.
Okay, so here's what's happening - the $post->authorName said: portion is displaying in italics but the $dataContent['message'] part is not. Both are enclosed in the <em> tag. Now, if I take the allowing of <p> tags away from the strip tags call, it all italicizes. I'm stumped - what would cause this to happen? I just want to keep the basic formatting of line breaks and still italicize the entire thing.
I just checked that <p> inside <em> is not 'italicized' :) but inside <i> does. Try it.

Categories