Reputation: 11
I am using fgetcsv to iterate through the file but the array returns strings with spaces that I cant map to an associative array. I tried to trim each string but its not working.
import class
*/
public function __construct($file = null) {
if(!empty($file)) {
$this ->read($file);
}
}
/**
* Read CSV file
* @param string $file CSV file to read
* @return boolean
*/
public function read($file) {
if(is_file($file)) {
$handle = fopen($file,'c+');
$rows = array();
$row = array();
$firstLine = true;
while(!feof($handle)) {
$row = fgetcsv($handle, 1000, "\t");
foreach($row as $val){
$trimmedArr = array();
$trimmedVal = trim($val, " ");
$trimmedArr[]= $trimmedVal;
}
// if (!$firstLine) {
if (count($trimmedArr) > 1) {
$rows[] = $trimmedArr;
// }
}
$firstLine = false;
}
$this -> _rows = $rows;
return true;
} else {
return false;
}
}
/**
* Set CSV fields to PHP array keys map
*
* Accepts an associative array with the CSV fields as keys and PHP array
keys as values.
* If CSV data is already loaded, will run the transformation on the data.
*
* @param type $map
*/
public function setAssociations($map) {
$this -> _fieldMap = $map;
if(!empty($this -> _rows)) {
$titleRow = array_shift($this -> _rows);
$rs = array();
foreach($titleRow as $pos => $title) {
if(isset($map[$title])) {
$rs[$map[$title]] = $pos;
}
}
foreach($this -> _rows as &$row) {
$associativeRow = array();
foreach($rs as $key => $pos) {
$associativeRow[$key] = isset($row[$pos]) ? $row[$pos] : '';
}
$row = $associativeRow;
}
}
}
/**
* Get data rows
* @return array
*/
public function getRows() {
return $this -> _rows;
}
The output array with spaces
row = {array} [27]
0 = "��T i m e "
1 = " C o u r s e "
2 = " P1 M e m b e r N u m b e r "
etc...
When I use my map array, it cant match the strings
$csv = new csv($fileTmp);
$map = array(
"Time" => 'time',
"Course" => 'course',
"P1 Member Number" => 'p1MemberNumber',
"P1 type" => 'playerType',
"P1 First Name" => 'firstName',
"P1 Last Name" => 'lastName',
"P1 Cart type" => 'cartType',
"P1 # of Holes" => 'numHoles',
"P2 Member Number" => 'p1MemberNumber',
"P2 type" => 'playerType',
"P2 First Name" => 'firstName',
"P2 Last Name" => 'lastName',
"P2 Cart type" => 'cartType',
"P2 # of Holes" => 'numHoles',
"P3 Member Number" => 'p1MemberNumber',
"P3 type" => 'playerType',
"P3 First Name" => 'firstName',
"P3 Last Name" => 'lastName',
"P3 Cart type" => 'cartType',
"P3 # of Holes" => 'numHoles',
"P4 Member Number" => 'p1MemberNumber',
"P4 type" => 'playerType',
"P4 First Name" => 'firstName',
"P4 Last Name" => 'lastName',
"P4 Cart type" => 'cartType',
"P4 # of Holes" => 'numHoles',
"Notes" => 'notes'
);
How do I change the fgetcsv output to regular strings?
Upvotes: 1
Views: 739
Reputation: 146460
Your CSV file is probably encoded as UTF-16 and the �� chars are the byte order mark. You can verify that with bin2hex() or just a proper text editor to inspect the file.
You can convert from Unicode to ANSI with iconv() or mb_convert_encoding() but you need to know beforehand what precise encodings you are using since there's no reliable way to auto-detect encoding programmatically—though a stream with a BOM is maybe the exception to this rule.
Upvotes: 2