I have .csv file, in that strings are looking like
"9.30 AM"
"Username"
"Message"
""
"9.30 AM"
"Username"
"Message"
""
Here I want to separate these and need to store in database withe separate columns. Means time will come to time column , username will its own column like. When "" comes need to check next line
means
"9.30 AM"
"Username"
"Message"
How do I do this?
I tried
echo startsWith($str, '|'); //Returns true
echo endsWith($str, '}'); //Returns true
You can open the file with file()
to return it as an array.
Then use array_chunk()
to split it in to separate messages and loop it.
I added an array_map to remove the "
around the values, but if it's not needed just use the commented line.
//$csv = file("filename.csv");
$csv = ['"9.30 AM"',
'"Username"',
'"Message"',
'""',
'"9.30 AM"',
'"Username"',
'"Message"',
'""'];
$messages = array_chunk($csv, 4);
foreach($messages as $mess){
list($time, $user, $m) = array_map(function ($a) { return trim($a, '"'); }, $mess);
//list($time, $user, $m) = $mess;
echo $time . " " . $user . " " . $m . PHP_EOL;
}
Insider the foreach loop you can use the variables to whatever you need.
https://3v4l.org/alctq