php csv转数组,php csv to array(csv 转数组)方法与代码

//ini_set('memory_limit', '-1'); // 如果csv比较大的话,可以添加。

/*

* $file : csv file

* $csvDataArr : header of csv table, eg: arary('name','sex','age') or array(0,1,2)

* $specialhtml : whether do you want to convert special characters to html entities ?

* $removechar : which type do you want to remove special characters in array keys, manual or automatical ?

* edit http://bbs.it-home.org

*/

class csv_to_array

{

private $counter;

private $handler;

private $length;

private $file;

private $seprator;

private $specialhtml;

private $removechar = 'manual';

private $csvDataArr;

private $csvData = array();

function __construct($file = '', $csvDataArr = '', $specialhtml = true, $length = 1000, $seprator = ',')

{

$this->counter = 0;

$this->length = $length;

$this->file = $file;

$this->seprator = $seprator;

$this->specialhtml = $specialhtml;

$this->csvDataArr = is_array($csvDataArr) ? $csvDataArr : array();

$this->handler = fopen($this->file, "r");

}

function get_array()

{

$getCsvArr = array();

$csvDataArr = array();

while(($data = fgetcsv($this->handler, $this->length, $this->seprator)) != FALSE)

{

$num = count($data);

$getCsvArr[$this->counter] = $data;

$this->counter++;

}

if(count($getCsvArr) > 0)

{

$csvDataArr = array_shift($getCsvArr);

if($this->csvDataArr) $csvDataArr = $this->csvDataArr;

$counter = 0;

foreach($getCsvArr as $csvValue)

{

$totalRec = count($csvValue);

for($i = 0; $i < $totalRec ; $i++)

{

$key = $this->csvDataArr ? $csvDataArr[$i] : $this->remove_char($csvDataArr[$i]);

if($csvValue[$i]) $this->csvData[$counter][$key] = $this->put_special_char($csvValue[$i]);

}

$counter++;

}

}

return $this->csvData;

}

function put_special_char($value)

{

return $this->specialhtml ? str_replace(array('&','" ','\'',''),array('&','"',''',''),$value) : $value;

}

function remove_char($value)

{

$result = $this->removechar == 'manual' ? $this->remove_char_manual($value) : $this->remove_char_auto($value);

return str_replace(' ','_',trim($result));

}

private function remove_char_manual($value)

{

return str_replace(array('&','"','\'','','(',')','%'),'',trim($value));

}

private function remove_char_auto($str,$x=0)

{

$x==0 ? $str=$this->make_semiangle($str) : '' ;

eregi('[[:punct:]]',$str,$arr);

$str = str_replace($arr[0],'',$str);

return eregi('[[:punct:]]',$str) ? $this->remove_char_auto($str,1) : $str;

}

private function make_semiangle($str)

{

$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',

'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',

'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',

'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',

'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',

'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',

'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',

'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',

'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',

'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',

'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',

't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',

'y' => 'y', 'z' => 'z',

'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',

'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',

'‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => ' '》' => '>',

'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',

':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',

';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',

'”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',

' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*');

return strtr($str, $arr);

}

function __destruct(){

fclose($this->handler);

}

}

// example:

$csv = new csv_to_array('user.csv');

echo ""; print_r($csv->get_array()); echo "";

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值