Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV reader – backslash is always treated as an escape character #492

Closed
rdarcy1 opened this issue May 8, 2018 · 1 comment
Closed

Comments

@rdarcy1
Copy link
Contributor

rdarcy1 commented May 8, 2018

This is:

- [x] a bug report
- [ ] a feature request

What is the expected behavior?

When required, the backslash should be treated as a normal character and not parsed as an escape character.

What is the current behavior?

The backslash acts as an escape character, so when at the end of a cell/field it escapes the closing quote and merges cells/lines.

What are the steps to reproduce?

<?php

require __DIR__ . '/vendor/autoload.php';

// CSV file with backslash at end of field
$contents = '"test field\"' . "\n" . '"another field"';
$filename = __DIR__ . '/test.csv';
file_put_contents($filename, $contents);


// Create a new reader
$reader = new PhpOffice\PhpSpreadsheet\Reader\Csv;

$spreadsheet = $reader->load($filename);

var_dump($spreadsheet->getActiveSheet()->getCell('A1')->getValue());

Output:

string(27) "test field\"
another field""

I know the backslash is commonly used as an escape character, but it would be good to at least have an option of turning this off, maybe with a $reader->setEscaping(false) method call. I'm currently dealing with third-party CSVs which contain backslashes (a poor design choice, but considering there's no universal standard I think it's perfectly valid).

RFC 4180 makes no mention of backslashes, using instead a quote character to escape quote characters.

Happy to put together a pull request if this change is deemed worthwhile.

@PowerKiKi
Copy link
Member

This seems to make sense. As you probably figured out we use PHP native fgetcsv and we don't expose its $escape parameter. So we you can create a PR for that is covered with unit tests, I'll merged it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants