I am using a TAB delimited file to import data in MySQL with PHP. My problem is whenever i display large strings (which are imported from Tab delimited files ONLY) which have spaces in them they won't wrap inside DIV,table cells e.t.c.
For example a big name like:
Mario Mark Le Blanc De Cooper VII
won't wrap inside a small DIV or table cell and instead will overflow and overlap nearby areas like other table cells. The wrapping problem occurs ONLY with data inserted from the tab delimited file.
Now the weird thing is after i go to PHPMyAdmin and manually remove all its spaces and re-add them, the word WILL wrap normally. I tried to import data from both ANSI and UTF8 encoded files but nothing changed.
I checked the ASCII code of the space character and it is indeed space.
I also tried str_replace to replace the string's spaces with new spaces but the problem persists
What about using preg_replace ? This might help you:
$yournewvariable= preg_replace('/\s+/','',$youroldvariable)
Try this:
$string = 'Mario Mark Le Blanc De Cooper VII';
$text = str_replace("\t", '', $string);
echo $text;
Try removing the actual tabs in the code.
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.
Input text :
Test one two three fourfive
Text is splitted by preg_split with additional processing and now result array is:
Test one
<span>two</span>
three
<span>four</span>
five
When I echo this array in loop I get this:
Test one two three four five
Four and five should be displayed together, without space.
In HTML source it looks like so:
Test one
<span>two</span>
three
<span>four</span>
five
Extra space is added after four.
When I directly write this in HTML
Test one <span>two</span> three <span>four</span>five
text is echoed correctly. It seems that new line in HTML add extra space. Does someone know what happens here?
According to W3C, newlines are whitespace characters and should be rendered as a space by browsers.
If you want to get rid of the space, use <pre> and format your text as needed, or modify your markup so you can left-float "five."
A newline is a space. Remove that and your space disappears.
Remove \n from your echo and it should be fine.
Just like OP suggest newline is whitespace too.
It still keeps the original text layout (I mean the spacing, offsets, new line, paragraphs) while the text fragment is stored in MySql ('text' type) field - I can tell when I peer into it in my DB browser (Adminer:)
but it gets lost when I output it from the DB: it becomes a single line string of my text characters. How can one restore it its original layout?
I've tried to reshape the text fragment using the PHP nl2br() function with some success:
it brought back the newline breaks, but the text words positioning is not kept, everything
shifts to the left.
Thanks in advance for a good idea.
If you've got multiple spaces and things like that. e.g. for code. Then trying using the pre tag.
http://htmldog.com/reference/htmltags/pre
http://reference.sitepoint.com/html/pre
The html_entity_decode() function converts HTML entities to characters.
The syntax is:
html_entity_decode(string, [quotestyle], [character-set]);
You can refer example2.
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 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.