php fgetcsv返回带空格的字符串数组

时间:2018-07-30 17:52:21

标签: php

我正在使用fgetcsv遍历文件,但是数组返回的字符串带有我无法映射到关联数组的空格。我试过修剪每根弦,但不起作用。

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;
}

带空格的输出数组

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 "

等...

当我使用地图数组时,它无法匹配字符串

$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'

 );

如何将fgetcsv输出更改为常规字符串?

1 个答案:

答案 0 :(得分:1)

您的CSV文件可能被编码为UTF-18,而^字符是byte order mark。您可以使用bin2hex()或仅使用适当的文本编辑器来检查该文件。

您可以使用iconv()mb_convert_encoding()从Unicode转换为ANSI,但是由于没有可靠的方法可以通过编程方式自动检测编码,因此您需要事先知道使用的是哪种精确编码,尽管BOM可能是该规则的例外。