Archive

Posts Tagged ‘explode’

php读取csv的函数

2011-07-12 留下评论

[cc lang=”php”]

function read_csv($cvs) {

$shuang = false;

$str = file_get_contents($cvs);

for($i=0;$i<strlen($str);$i++) {

if($str{$i}=='”‘) {

if($shuang) {

if($str{$i+1}=='”‘) {

$str{$i} = ‘*’;

$str{$i+1} = ‘*’;

} else {

$shuang = false;

}

} else {

$shuang = true;

}

}

if($str{$i}==’,’) {

if($shuang) {

} else {

$str{$i} = ‘|’;

}

}

if($str{$i}==”\n”) {

if($shuang) {

$str{$i} = ‘^’;

} else {

}

}

}

$str = str_replace(array(‘”‘,’*’),array(”,'”‘),$str);

$a1 = explode(“\n”,$str);

$array = array();

foreach($a1 as $k=>$value) {

if($value) {

$value = str_replace(“^”,”\n”,$value);

$array[$k] = explode(“|”,$value);

}

}

return $array;

}
[/cc]

Advertisements