Asheeown Posted January 29, 2008 Share Posted January 29, 2008 Hello, I have a script that is taking two rows from a csv file, exploding them with the delimiter "," changing a few columns simply by overwriting them, ex. $Data[136] = "New Data" At the ends at about column 156 their is text fields instead of integers, and some of them have commas in them, now when I implode the string it takes those and splits it, which I cannot have. Is their anyway I can tell php to ignore any commas used inside a variable in the $Data array? Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/ Share on other sites More sharing options...
Asheeown Posted January 29, 2008 Author Share Posted January 29, 2008 For more information on the topic, if it were to actually extract correctly columns, 157 and 174 would be the ONLY ones that would have commas in them at anytime, is their anyway to just rig up a bypass just for those two? I wish the file was compressed with semi-colons instead, however excel and CSVed are able to distinguish the different between a column and a regular comma in a row, how is that? Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-451969 Share on other sites More sharing options...
resago Posted January 29, 2008 Share Posted January 29, 2008 is the text wrapped in quotes? if so, you could do a preg_split and split on , but not on "..., .." Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-451983 Share on other sites More sharing options...
resago Posted January 29, 2008 Share Posted January 29, 2008 $a=preg_split('/","/',$string); that should do it. Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-451995 Share on other sites More sharing options...
Asheeown Posted January 29, 2008 Author Share Posted January 29, 2008 I'll try it out, I don't know if the text is wrapped in quotes it's a normal csv file. Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-452579 Share on other sites More sharing options...
Asheeown Posted January 31, 2008 Author Share Posted January 31, 2008 This wouldn't work to begin with, when I do the command fgetcsv I use the delimiter "," and it has to be that way and that's when the columns get added. Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454653 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 u prolly will need a custom function to seperate the fields. function csv_array($file,$delim) { $fh=fopen($file,"r"); while(!feof($fh)) { $line=fgets($fh); $row=explode($delim,$line); foreach($row as $key => $val) { if(($ch=substr($val,0,1))=='"' || $ch=="'") { // we got a string delimeter $skey=$key+1; do { $sl=strlen($val); if(substr($val,$sl-1,1)==$ch) break; // we got a matching delimeter $val .= $row[$skey] unset $row[$skey++]; } while(true); $sl=strlen($val); $row[$key]=substr($val,1,$sl-2); // remove the quote delimeter } } $newrow[]=$values($row); // Rebuild our array removing empty sets (ones we removed with unset); } fclose($fh); return $newrow; } now not shure, but the unset command may have to be outside the foreach loop, depends on how foreach works internally. if that is they case, create a new array with those items for removal. this code not tested, just code that got pumped out of my head into this post; Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454696 Share on other sites More sharing options...
Asheeown Posted January 31, 2008 Author Share Posted January 31, 2008 <?php function csv_array($file,$delim) { $fh=fopen($file,"r"); while(!feof($fh)) { $line=fgets($fh); $row=explode($delim,$line); foreach($row as $key => $val) { if(($ch=substr($val,0,1))=='"' || $ch=="'") { // we got a string delimeter $skey=$key+1; do { $sl=strlen($val); if(substr($val,$sl-1,1)==$ch) break; // we got a matching delimeter $val .= $row[$skey]; unset($row[$skey++]); } while(true); $sl=strlen($val); $row[$key]=substr($val,1,$sl-2); // remove the quote delimeter } } $newrow[]=$values($row); // Rebuild our array removing empty sets (ones we removed with unset); LINE 23 } fclose($fh); return $newrow; } $Array = csv_array("Spells.dbc.csv",","); print_r($Array); ?> Gives me: Fatal error: Function name must be a string in /home/Development/htdocs/Php/SpellMaker/test.php on line 23 I specified what line 23 is in the code. If I change "$newrow[]=$values($row);" to "$newrow[]=$values[$row];" or "$newrow[]=$val[$row];" it still gives me other errors or doesnt set the array with any values, 11 fields show in the array though, that is how many are in the csv file, just nothing set to them Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454753 Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 whoops, that shudda been array_values Quote Link to comment https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454787 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.