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

Feature request: Trim parsed csv strings #517

Closed
PetrKaterinak opened this issue Jan 31, 2024 · 2 comments
Closed

Feature request: Trim parsed csv strings #517

PetrKaterinak opened this issue Jan 31, 2024 · 2 comments

Comments

@PetrKaterinak
Copy link

PetrKaterinak commented Jan 31, 2024

Feature Request

Q A
New Feature yes
BC Break no

Proposal

It would be nice to have possibility to automatically trim() parsed strings. Something like

$parsedCsv = \League\Csv\Reader::createFromPath($path, 'r');
$parsedCsv->trimStrings()

So I dont need to use this code each time I'm parsing CSVs:

function trimArrayStrings(&$array) {
    foreach ($array as &$value) {
        if (is_array($value)) {
            trimArrayStrings($value); // Recursive call for nested arrays
        } elseif (is_string($value)) {
            $value = trim($value); // Trim the string
        }
    }
}
@nyamsprod
Copy link
Member

The Reader has a addFormatter method where you can register a callback to format the record before it is accessible by any other mechanism. Nothing prevents you do to something like this.

$reader = Reader::createFromPath($path);
$reader->addFormatter(fn (array $record): array => array_map(trim(...), $record));
foreach (reader as $record) {
// 
}

Hope it will help you.

@PetrKaterinak
Copy link
Author

PetrKaterinak commented Jan 31, 2024 via email

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

No branches or pull requests

2 participants