Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/88319-csv-import-help/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-451969
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454696
Share on other sites

<?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

Link to comment
https://forums.phpfreaks.com/topic/88319-csv-import-help/#findComment-454753
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.