首页 > PHP > php读取csv的函数

php读取csv的函数

[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]

  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: