dennismonsewicz Posted September 18, 2009 Share Posted September 18, 2009 $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, ";")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo substr_replace($data[$c], ",", -1); } } fclose($handle); The code above takes a csv file and dumps the results to the screen... I am wanting to strip the final comma off of the result: normal output: INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' ');, what my code outputs: INSERT INTO `portal_deductible` VALUES(,'att,'AT&T ,'5700,'50,' ,' ,' '), Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/ Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 what should the output look like? Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920784 Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2009 Author Share Posted September 18, 2009 INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' '); so just like the normal output from above, without the trailing comma Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920786 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 see if this function works for you function removeLast($string, $char){ $pos = strrpos($string, $char); $newString = substr_replace($string, '', $pos, strlen($string)); return $newString; } with the following code $string = "INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' ');,"; echo removeLast($string, ','); the output is INSERT INTO `tblName` VALUES(1,'att','AT&T ','5700','50',' ',' ',' '); Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920793 Share on other sites More sharing options...
Mark Baker Posted September 18, 2009 Share Posted September 18, 2009 Why can't you just implode the csv array back to a string? while (($data = fgetcsv($handle, ";")) !== FALSE) { $dataLine = implode(',',$data); echo 'INSERT INTO `tblName` VALUES('.$dataLine.')'; } Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920797 Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2009 Author Share Posted September 18, 2009 here is my code function removeLast($string, $char){ $pos = strrpos($string, $char); $newString = substr_replace($string, '', $pos, strlen($string)); return $newString; } $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, ";")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo removeLast($data[$c], ','); } } fclose($handle); nothing echoes out on the screen now Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920799 Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2009 Author Share Posted September 18, 2009 @Mark Baker: The CSV file contains the insert into sql script sample of the csv: INSERT INTO `tblName` VALUES(1, 'att', 'AT&T ', '5700', '50', ' ', ' ', ' '); INSERT INTO `tblName` VALUES(2, 'att', 'AT&T ', '8525', '125', ' ', ' ', ' '); INSERT INTO `tblName` VALUES(3, 'att', 'AT&T ', 'TILT (8900) ', '50', ' ', ' ', ' '); INSERT INTO `tblName` VALUES(4, 'att', 'AT&T ', 'TILT (8925) ', '125', ' ', ' ', ' '); INSERT INTO `tblName` VALUES(5, 'att', 'AT&T ', 'USBCONNECT 881 ', '50', ' ', ' ', ' '); INSERT INTO `tblName` VALUES(6, 'att', 'AT&T ', 'USBCONNECT MERCURY ', '50', ' ', ' ', ' '); Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920801 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 hmmm Idk why it isn't echoing anything. it works for me on my machine. do a print_R on your data array or echo $data[$c] before you do the function and make sure the right data is in there Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920802 Share on other sites More sharing options...
dennismonsewicz Posted September 18, 2009 Author Share Posted September 18, 2009 print_r on $data[$c] prints the data out, but for some reason the function does nothing for me code: function removeLast($string, $char){ $pos = strrpos($string, $char); $newString = substr_replace($string, '', $pos, strlen($string)); return $newString; } $row = 1; $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, ";")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { //print_r(removeLast($data[$c], ',')); //print_r($data[$c]); } } fclose($handle); Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920865 Share on other sites More sharing options...
knsito Posted September 18, 2009 Share Posted September 18, 2009 erm It is a bit confusing what you are trying to get at if your data is delimited by ";" try $data = fgetcsv($handle, 0 , ";") and none of that substr_replace stuff to get the ";" back $data[0] = $data[0].";"; dunno what you are trying to do with echo "<p> $num fields in line $row: <br /></p>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/174722-substr_replace-help/#findComment-920890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.