AdRock Posted March 15, 2008 Share Posted March 15, 2008 I have a CSV file and I want to get each part of the line of text into different variables I have found some code but can't get it to put into seperate variables I have a form with 2 fields where the user can add there own data but if they click a button iwant this function called function get_start() { $file_handle = fopen("test.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); $s_lat == $line_of_text[0]; $s_long == $line_of_text[1]; } fclose($file_handle); } here is a cut down version of my code $s_lat = $_POST['s_lat']; // what the user can enter $s_long = $_POST['s_long']; // what the user can enter //variables for end of route $e_street = $_POST['e_street']; $e_postcode = $_POST['e_postcode']; $e_lat = $_POST['e_lat']; // what the user can enter $e_long = $_POST['e_long']; // what the user can enter //variable for departure time $depart = $_POST['time']; function get_start() { $file_handle = fopen("test.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); $s_lat == $line_of_text[0]; $s_long == $line_of_text[1]; print $s_lat." ".$s_long; } fclose($file_handle); } function get_end() { $file_handle = fopen("test.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); $e_lat == $parts[0]; $e_long == $parts[1]; } fclose($file_handle); } Link to comment https://forums.phpfreaks.com/topic/96238-getting-csv-text-into-variables-so-i-can-insert-into-database/ Share on other sites More sharing options...
discomatt Posted March 15, 2008 Share Posted March 15, 2008 This regex isn't by me, so I can't gaurantee anything.. <?php $contents = file_get_contents('path/to/your/csv.txt'); preg_match_all('/(?:^|,)(\\"(?:[^\\"]+|\\"\\")*\\"|[^,]*)/', $contents, $matches); print_r($matches); ?> Link to comment https://forums.phpfreaks.com/topic/96238-getting-csv-text-into-variables-so-i-can-insert-into-database/#findComment-492646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.