PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Arrange array from string csv

 February 12, 2022     laravel, php     No comments   

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'), '|');

result

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing