I've bumped into a small problem that i cant seem to find the fix for.
I've got this simple form with a textarea, no biggie.
The problem shows when i edit a post i've already got in the database.
So, i retrieve the varchar field i have in my database and inserts it into my form by default.
If this is what i got by default:
this is some default text
with a line break
now, if i just saves this without doing any changes, this is what i get in the PHP $_POST variable:
this is some default text
with a line break
the /n character seem to multiply by itself. The next time i save the text the /n multiply again, giving me 4 /n in a row and so on.
It doesn't make any sense to me.
Step 1. I pre-fill the textarea with the contents mentioned in the first gray box above
Step 2. I submit the same content
Step 3. I run the following code before i do anything else:
echo '<pre>'. $_POST['content'] .'</pre>'; die;
Step 4. The newline are multiplied as in the second gray box above
That's easy.
there is some code in your application, that doubles line breaks. Just find it and get rid of it. Not a big deal.
(If you don't trust me, as you obviously do, create a simple testing script consists of just form and nothing else and see)
Wild guess: Windows CR LF line breaks.
In Windows, if you type a line break, they consist of not only \n, but \r\n. If MySQL implicitly converts \r to \n when inserting/updating, this would explain this behaviour.
Try removing any occurences of \r in the PHP first and see if that helps.
Remove line break using trim
Related
i wonder how to store the values passed by html form as it is interms of format into mysql table. can anyone help me.
let me clear my question.
when the user enter his address in the text area like,
no:14,cross cut road,
XXX nagar,
Tamilnadu,
india.
and when this value is passed from a form and stored in mysql table, it'll displayed in a single line when i retrieved it in the next time, like
no:14,cross cut road,XXX nagar,Tamilnadu,india.
but i want to display it like the previous one like how they enter, in the same format.
can anyone help how to do this???
thanks in advance.
i've no idea to use any web editors. so i tried n2lbr function and got the same format. New line problem solved but now am facing the space problem. if i leave 10 spaces and wrote a text, it'll display the text only.. not the spaces. how to solve this??
You should use a wysiwyg editor like ckeditor.
Use
mysql_real_escape_string doesn't remove line breaks, it escapes them.
While displaying use
nl2br for adding those line breaks.
when you press enter in the text area the '\n' character is appended to your text and this character is stored in DB but when you read from the DB and want to visualize your text, you should consider that your component recognize the '\n' character, if it doesn't recognize newline character (i.e. '\n') then you should replace '\n' with html break (i.e. br) tag to show the new line in html.
I have text within one of my tables which is a long description, but the text is all clumped together. I need a way for PHP to find each full stop and add a line break after it, so I can treat the description as a list.
A better solution would be to add an tag at the start of each sentence, and have each sentence finish with a so I can actually add bullet points.
Thanks.
You can use the str_replace function to replace each occurrance of a full stop with a full stop plus a line break. If you are rendering HTML, a line break is probably the <br/> tag.
I am writing a form using php the form has a textarea and the data is sent to the script using $_POST; then stored into db.
The problem is that when the textbox is populated and if the user do not press enter to make a new line break but he wait until the end of the width of the textarea the text stored in the db is all in one line, for example if he wrotes:
HELLO THIS IS TEXT AND IT COVERS ALL THE WIDTH OF THE TE<=end of textbox (no br no /n)
XTAREA.
This even if when you reach the maximum width of the textbox the text goes in a new line.
when I print the message with a query the result is all in one line (which screw up my site layout)
I have managed to record user ENTER strokes by using this code:
$cleaned_message=str_replace(Chr(13),'<br>', $cleaned_message);
But i cannot figure out what to do if he doesn't use ENTER KEY.
What you describe in first part of your post is a normal and correct behavior. Text is wrapped in the textarea and as long as user does not hit enter, it's interpreted as one line. It is the user to specify where he wants to have a new line, not the textarea.
What's broken is most probably your layout. Try to define width of the output element (I guess this is also a textarea), so the output is wrapped in the element, rather than element adjusted to the output.
Could you update your post with a part of the layout where the result is printed?
If the user does not use the Enter key (and does not otherwise insert line breaks, e.g. by copying and pasting text that has line breaks), then there are no real line breaks in the data, as stored in the DOM and as submitted in the form data. Browsers divide the text in several lines as needed to make it fit, but the line breaks are “soft,” just visual rendering.
The user-entered line breaks are transmitted as CR, LF pairs (Carriage Return, Linefeed). In the default form data encoding, this means %0D%0A. What you do with them depends on the context. Line breaks might be retained and interpreted as paragraph breaks or as content-significant line breaks (e.g., in postal addresses, poems, and computer code), or they might be replaced by spaces.
There is no way and no need to deal with browser-generated line breaks server-side, since they don’t reach the server.
However, if the textarea element has the (nonstandard) attribute wrap=hard, then the browser-generated line breaks become real line breaks (and they are indistinguishable from user-entered line breaks). The attribute wrap=off prevents automatic wrapping: a line can be arbitrarily long, and horizontal scrolling appears as needed.
I am new to php mysql.
I have a textarea and a button. When the user clicks the button, the value in the textarea is stored in the mysql database. The problem i am facing is...
If the user enters text like
hey this is my text (line break)
and this is my second line text
...the database stores it like:
hey this is my text and this is my
second line text
where (linkbreak) which is actually line break by pressing enter key. I add
pre tag before saving it in database it works fine but all of its css formating like bold font size and font family gets lost..
how can i solve this problem..
When you store text in a database, think of it as a string of individual characters with no presentational meta data attached (as in, what you see in the textarea that is submitted is not necessarily contained in the string that is posted).
The ability to see it in any format depends on context. The \n will be there, it just needs to be converted into <br /> to be seen on a web page (or in pre or element with white-space: pre).
Also, any colors or other presentational things will be dependent on the CSS used when outputting the string again.
You could also use nl2br($stringfromwebsite) to change all newlines to <br />'s
If you print out the text in PRE and it is displayed with line breaks, then the line breaks are retained. It’s just that HTML doesn’t regard about them in the normal text flow:
Line breaks are also white space characters. Note that although
and
are defined in [ISO10646] to unambiguously separate lines and paragraphs, respectively, these do not constitute line breaks in HTML, nor does this specification include them in the more general category of white space characters.
[…]
For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.
That’s the reason why HTML introduced the BR elements that represent visual line breaks. You can use nl2br that inserts BR elements in front of each line break character sequence.
I have a php data migration script that serializes an array of text. Somehow, and this is not in my codebase, but the script is inserting \\\\\\\r\\\\\\\n into the string. Here is an example of what the output is like:
Product Line: [56313] LEGO Batman Screenshot\\\\\\\\r\\\\\\\\n[5
6384] LEGO Batman Screenshot[56446] LEGO Batman Screenshot[56460] LEGO Batman: T
There are no line breaks in between the different products (as you can see, there are two products being shown there - that's two iterations). When the second product gets appended to the first, the \\\\\\r\\\\\\n gets prepended. This question is fairly difficult to explain, and I don't really think that any code will help explain it. What is this and how do I get rid of it? I'm on a windows platform.
You can call trim(), http://us.php.net/trim
Hard to tell what's going on without the code, but I would search the code for \r and \n and try and find out where this is being inserted.
There are various ways to trim that string back to normal, but I suggest you search for the cause of why this happens.
The many backslashes look like the result of a (multiply) botched addslashes() operation, or a string being mangled by magic_quotes_gpc. The data definitely started with a line break somewhere along the line - if you got the data from a text file, presumably from there. These are two separate issues, even if you manage to remove the newlines, the slashes issue remains.
You would have to show us more code, or examine it using test output or a debugger.