I am trying to parse a set of CSV data using PHP, but there is a big problem. One of the fields is a long description field, which includes line breaks within the enclosures itself.
My primary digit is writing a piece of code that can divide the data line by line, but also recognizes that the line breaks should not be used within the data. Within this area, line breaks are not well-protected, making them difficult to differentiate from a legitimate line break.
I have tried to come up with a regular expression which can handle it properly, but there is no luck till now. Any ideas?
CSV format:
"####", "Text Data Here", "Text Data \ nLinibrics \ nhere" \ n "#### "," More text data "," more data \ n \ n line breaks \ n here "\ n
PHP's CSV handling content is non-standard and is contradictory with RFC4180, thus fgetcsv () [Can not Transact properly] from the files [at the end of that line
And to get this limit, they offered the following function:
function $ cnt = strlen ($ string); $ Esc = false; $ Escesc = false; $ Num = 0; $ I = 0; While ($ i & lt; $ cnt) {$ s = $ string [$ i]; If ($ s == $ CSV_LINEBREAK) {if ($ esc) {$ O [$ num] = $ S;} and {$ i ++; Break;}} Otherwise ($ s = $ CSV_SEPARATOR) {if ($ esc) {$ o [$ num] = $ S; } And {$ num ++; $ Esc = false; $ Escesc = false; }} Otherwise ($ s == $ CSV_ENCLOSURE) {if ($ escesc) {$ o [$ num] = $ CSV_ENCLOSURE; $ Escesc = false; } If ($ esc) {$ esc = false; $ Escesc = true; } And {$ esc = true; $ Escesc = false; }} And {if ($ escesc) {$ o [$ num]. = $ CSV_ENCLOSURE; $ Escesc = false; } $ O [$ num] = $ S; } $ I ++; } // $ string = substr ($ string, $ i); $ O flame; } It seems that it will do the trick.
Comments
Post a Comment