Issue
I need to transform my base64 CSV string, to a line where I take $line[0], $line1 etc..
// my file uploaded
"filestream": "Q29kaWNlIEV2ZW50b3xJRCBQYXJ0ZWNpcGFudGV8VGlwbyBwYXJ0ZWNpcGFudGV8Tm9tZSBwYXJ0ZWNpcGFudGV8Q29nbm9tZSBwYXJ0ZWNpcGFudGV8Q29kaWNlIGZpc2NhbGV8UVIgQ29kZXxTdGF0byBDaGVjay1pbnxOb3RlIENoZWNrIGlufFVzZXIgY2hlY2staW4KVGltZXN0YW1wIGNoZWNrLWlufFRpbWVzdGFtcCBlc3BvcnRhemlvbmUKNDIxfDEyMjA1fE9zcGl0ZSBkaSBzZWRlfFNhYnJpbmF8UnV6emF8UlpaU1JOODBBMDFHMjI0SHwwMDQyMTVMTk41MDAwMTIyMDV8UHJlc2VudGV8fEV4dC5Qcm92aWRlcnwyMC8wMS8yMDIyIDAxOjE3OjU0fHwKNDIxfDEyODk1fEFnZW50ZXxSaWNjYXJkb3xCb2dvZ25hfHwwMDQyMXRBR2w0MDAwMTI4OTV8QXNzZW50ZXxOb24gcHJlc2VudGUgYWxsJ2V2ZW50b3x8fHwKCgo="
I give back from: $csvFile = str_getcsv($request->json('filestream'), '|');
the result expected is: Array( array(row), array(row2), array(row3) )
Solution
str_getcsv()
expects a single line, so you'll have to base64 decode the original string, then split that by newline to get multiple lines, then loop over those with str_getcsv()
to get the array for each line. Something like this:
$array = [];
foreach (explode("\n", base64_decode($string)) as $line) {
$array[] = str_getcsv($line, '|');
}
print_r($array);
You might add some extra checks to skip the blank lines in the input if those are not significant.
Answered By - Alex Howansky
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.