Forum Moderators: coopster
1,Test in Progress,2,3,4, , , ,
2, , , , ,Next Test,5,6,
What I need to do is get the line into an array so that I can then process it depending on the first value on the line. The processing involves inserting the data into a database table, the table being dependant on the value.
I think I need to use preg_replace or preg_split to split the line into an array but even after reading the manual and searching other posts here I cannot seem to get the syntax correct. Can anyone point me in the right direction?
2,,,,,"Next Test",5,6,
where any text is quoted and could possibly contain commas and numerals will not be quoted. How do I now get the text parts out without it splitting at any commas that might be there?
Thanks
Phpmyadmin has this functionality, either copy their code, or using phpmyadmin, insert the CSV into a tempory table, and then using php, select all the records and do what you want with them.
(replace the ¦ with the proper pipe)
Untested, but I don't think I've made any errors...
Then
$out[1][0] will be the first field,
$out[1][1] will be the second field,
etc
Shawn
<added: Just to explain what it is doing so you can do future maintenance:
[^,]\"(.*?)\"[,$]¦[^,](\d*)[,$]
means either [^,]\"(.*?)\"[,$] or [^,](\d*)[,$]
[^,]\"(.*?)\"[,$] means:
In the second part, \d matches a digit, and * again means 0 or more times.
It almost works but not quite. I have not tried to get it to insert anything into the database yet, I am just trying to echo the elements of the array to make sure that they are there.
I tried
if ($out[1][0] == 1)
{
code to echo array elements
}
elseif ($linea[1][0] == 2)
{
code to echo array elements
}
and nothing happened.
I then tried
if ($out[0][0] == 1)
{
code to echo array elements
}
elseif ($linea[0][0] == 2)
{
code to echo array elements
}
which at least resolves the if statement to true when it is true but I must be doing something wrong with trying to get the array elements to display in the browser.
print "$out[0][1] Parameters $out[0][2], $out[0][3], $out[0][4]<br>";
results in
Array[1] Parameters Array[2], Array[3], Array[4]
being displayed in the browser. Any ideas?
I'm feeling very stupid this evening after a very long day so any help will be much appreciated.