I am trying to parse this xml document in which a newline is required for certain fields and must be inserted into the database with the newline. But I've been running into problems.
1)First Problem: \n Character
The first problem I had was using the \n like below.
<javascript>jquery_ui.js\nshadowbox_modal.js\nuser_profile.js\ntablesorter.js</javascript>
The problem was in the database the field came out ot be jquery_ui.js\nshadowbox_modal.js\n... and when output into html it was jquery_ui.jsnshadowbox_modal.jsn...............
2) Then I tried actually having newlines in the xml
<javascript>jquery_ui.js
shadowbox_modal.js
user_profile.js
tablesorter.js</javascript>
The problem was the output become %20%20%20%20%20%20%20%20%20%20shadowbox_modal.js, and so forth. So how can I get a newline to hold from xml when entered into a database and then output with the newline still?
Remove the spaces from your second example.
You probably entered a tab and/or spaces for readability, but these get inserted too.
%20 is an urlencoded space.
Related
I have a PHP script that displays an HTML form that allows a user to enter data in a <textarea> and store it in MySQL.
The user (me) entered multiple lines of text in the <textarea> that included \n, \r, and 4 consecutive space characters (to indent a list I was making). For example:
first line of text:
second line of text:
(A) some task
(B) another task
When the form was submitted, the content was stored correctly in MySQL, including the hidden \r, \n, and the four space characters before (A) and (B).
However, var_dump()-ing the data in PHP shows this:
first line of text.\r\nsecond line of text:\r\n (A) some task\r\n (B)another task
The four space (U+0020) characters I entered do not appear between the \r\n and (A), there is only one space character. I even ran the data through a string to hex converter and it only showed one U+0020 before each (A) and (B) instead of four U+0020s. But, when I re-open the form to edit the data in the <textarea>, the content shows up correctly, just I had entered it originally, with the 4 spaces before the (A) and (B).
My scripts all are behaving correctly and there is no problem. I'm just wondering: How is MySQL and <textarea> able to detect the 4 spaces, but var_dump() only detects one space?
Here is what I have tried to detect the 4 spaces in PHP, with the data stored in a PHP $Variable:
var_dump($Variable)-ing immediately before and after storing the data in MySQL, before/after stripslashes(), and before/after outputting the data to <textarea> and all each var_dump() does not detect detect the 4 spaces.
strpos($Variable) detects the 4 spaces.
print_r($Variable) does not detect the 4 spaces.
echo $Variable does not detect any hidden characters, except for single spaces (not the 4 consecutive spaces)
When you var_dump a variable, it is shown the same HTML.
If you want to show the spaces, you can add white-space:pre-wrap.
The white-space CSS property determines how whitespace inside an element is handled. To make words break within themselves, use overflow-wrap, word-break, or hyphens instead.
With pre-wrap, sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.
I have a problem: so I have a data thats coming out of the database and stored in a text area. When the user changes the data in the text area, the content is sent to javascript and via AJAX (POST to a PHP script) the database is updated. This works fine until the user starts adding newlines. Then javascript transforms this into a \n-character and thus it gets stored in the database as \n.
What I want is to have actual newlines in my database and not the \n newline-characters. Is there any way that I can use php to replace the \n with an actual newline (NOT a br)? I have tried altering the database field after the edit with the char(10), but for some reason this is not working in the script except when I do it manually in phpmyadmin?
When editting with a full php request, the newline in a text area is correctly stored as a char(10) in mysql, not as \n.
Anyone got a clue?
Store it as it is but escape first with real_escape_string
real_escape_string converts what is a newline into the 4 character string '\n\r'
$text = $mysqli->real_escape_string($text);
Use [nl2br][1] function to replace /n with newline
Insert line breaks where newlines (\n) occur in the string:
<?php
echo nl2br("One line.\nAnother line.");
?>
The browser output of the code above will be:
One line.
Another line.
I'm trying to dynamically generate a csv file with some cells that will contain multiple lines, the address field for example will need to be grouped into a single "address" cell instead of address,city,state etc. All is going well and but for the last two days i've tried to insert \r, \r\n, \n, chr(10),chr(13), as well as a carriage return within the code to create the carriage return i'm looking for within the cell. All of these fail, either being literally printed in my csv as "\r" etc or when I do a manual carriage return in the code it generates a new row. I'm using this to create the breaks in my cells but it isn't working
$groupedCell = implode('\r',$data);
I'm pretty sure the code is correct as its placing \r where I would like a carriage return but not the actual return i'm looking for. I've tried some different encodings but still no luck, I am testing in Open Office which I guess could be the issue but I would assume it can handle carriage returns within a cell and I haven't seen any documentation to support otherwise. Thanks for reading!
The CSV spec is one I find implemented in many different ways... it basically seems like it's only half-speced which is frustrating given it's popularity.
To include a new-line within a cell in a CSV there cell may need to be wrapped, or the new-line may need to be escaped. You'll notice from the linked doc there are three ways to do this - and different programmes treat it differently:
Excel wraps the whole cell in double quotes: a cell can have (unescaped) newline characters within it and be considered a single cell, as long as it's wrapped in double quotes (note also you'll need to use excel-style double quote escaping within the cell contents)
Other programmes insert a single backslash before the character, therefore a line ending in \ is not considered the end of a line, but a newline character within the cell. A cell can have unescaped newline characters within as long as they're preceded by the backslash character.
Others still replace a newline with C-style character escaping, the actual character sequence \n or \r\n. In this case the cell has fully escaped newline characters.
The problem is compounded by the potential need to escape the control characters (as well as other content (eg " in #1, and \ in #2+3) and different styles of escaping (eg. an embedded quote could be escaped as: double double quote "" or backslash-double quote \")
My advice: generate an open-office document with multiple lines and key escape characters and see how open-office generates a CSV file. From there you can decide which of the above methods to use for newlines within cells, and which escaping method.
example of style-1 (excel):
#num,str,num
1,"Hello
World",1990
2,"Yes",1991
example of style-2:
#num,str,num
1,Hello \
Word,1990
2,Yes,1991
example of style-3:
#num,str,num
1,Hello \nWorld,1990
2,Yes,1991
You need to use "\r". You can't use escaped characters (aside from \') in single quoted strings. '\n' and '\r' are a literal backslash followed by an n or r, while "\n" and "\r" are newlines and carriage returns respectively.
As for inserting new lines in your CSV file, it's up to your implementation. There is no standard for CSV, so you'll have to figure out what format to use based on the system you're supplying the CSV data to. Some might accept a '\n' sequence and interpret it as a new line, others might allow a literal newline provided the cell is enclosed in quotes, and still others will not accept new lines at all.
Created an Excel 2010 worksheet with 3 columns.
Added a heading row with literal values: one, two, three
Added 1 data row with literal values: abc, abc, abc except that within the 2nd column I pressed ALT+ENTER after each letter to create a carriage return and line feed.
Did SAVE AS > OTHER and choose CSV while ignoring the warnings.
Examined the CSV data using NOTEPAD++ and clicked the Show All Characters button in toolbar.
One can see the following:
one, two, three[CR][LF]
abc,"a[LF]
b[LF]
c",abc[CR][LF]
Hope this lends more clarify.
I'm working on one application ( using PHP, javascript ). Below is the short description about my problem statement
There are two forms avaliable on my application, i.e. SourceFrm and targetFrm.
I am taking input on first form i.e. SourceFrm and doing processing on targetFrm.
Below is the input which I am taking from SourceFrm :
1) Enter your data (Identification of this input box id is 'inputdata' ):
2) Enter id ( Identification input box id is id ):
As per above input feed by user I am posting this data to targetFrm for further processiong.
On TargetFrm :
I am simply assigning inputdata value to php varible.
The spaces which are in between of words are getting lost ( more than one spaces converting to one space).
e.g.
User has added below data on input box and submitted
inputdata:
This is my test.
Here observed that user has added 5 spaces in between 'my' and 'test' word.
After assigning this input data to php variable. After that I printed this value
Below content I am getting
Output:
This is my test.
More than one spaces is converting to one space. This behaviour I checked on all browsers like FF,MSIE7/8 opera, safari, chrome.
If have used '<pre>' before printing php variable i.e.:
print "<pre>";
print $inputdata;
At time spaces are not getting lost (I am getting exact content).
Here my conflict is how do I presrve exact contents without using '<pre>'.
I have used encoding/decoding (htmlentitiesencode() and decode () )functionality, in my further data processing, so it may create some conflict if i replace spaces with . ( May conflict ll occur if i use instead space ).
Is anyone has any ideas, suggestions please suggest.
-Thanks
When you output your variables to HTML, they are parsed as HTML. Any additional white space is brought down to one space.
A simple fix would to replace all spaces with the html entitity to force browsers to display each space.
I wouldn't store the string with all the &nbps; in the database, but when you show it the would ensure that each space is seen.
EDIT
I mean only replace spaces on render...like:
print str_replace(' ', ' ', $inputdata);
HTML is capable of showing only one space. I'm not really sure why, but if you check your source code of rendered webpage containing your string, you'll see that it contains all the space, the browser just doesn't show it.
The same is for other space characters, as tabs.
The way to deal with it depends on type of your content. You can either replace spaces with or leave it as it is or do something completely different, i.e. strip more than one space down to one space.
It really depends on naturel of your data–the only real situation, when you would need more spaces than one, that comes to my mind is if you're trying to indent things with spaces, what actually isn't that great idea.
Edit: older resource:
http://www.sightspecific.com/~mosh/WWW_FAQ/nbsp.html
I have a textarea that I need to put a new line into with some dashes above. I have tried nl2br but that just echos the <br> tag. I have also tried to concat the \n but it is ignored.
What this is for is an email like system. When the user replies to an email, I want the old message below with a seperator like a few dashes. I can't get this to work though.
new message starts here
--------------
old message here
Can someone please give a hand?
Thanks.
Try a return character: \r
According to this article, the \n newline character should work. What is happening when you are inserting the \n character into the string using double quotes?
This works for me:
<textarea>test
testing</textarea>
it properly creates the next line. Check to make sure your source code looks something like that, with n actual line break in the source where you want it to be.