程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> PHP讀取CSV文件代碼

PHP讀取CSV文件代碼

編輯:PHP基礎知識
 

PHP讀取CSV文件,由於Excel最多只能顯示65536行,數據量大不能完全顯示
CSV文件直接導入Mysql不好使, 於是采用PHP直接CSV文件今天發現fgetcsv()函數操作CSV文件讀取入庫

代碼還沒做整理,

代碼如下
<?php
set_time_limit(0);

$host = 'localhost';
$user = 'root';
$pswd = '';
$dbname = 'email';

$emailArr = array('admin', 'administrator', 'contact', 'info', 'support', 'webmaster');

mysql_connect($host,$user,$pswd) or die('connect error');
mysql_select_db($dbname) or die('Select db error');

function randStr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
mt_srand((double)microtime()*1000000*getmypid());
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}

$csvFileName = '2.csv';
$row = 0;
$handle = fopen($csvFileName,"r");
while ($data = fgetcsv($handle, 1000, ",")) {
//$sql = mysql_query("INSERT INTO email SET email = '$data[1]'");
$rand = randStr(15);
$row++;
$domain = $data[1];
for ($i = 0; $i < count($emailArr); $i++) {
$email = $emailArr[$i].'@'.$domain;
$sql = mysql_query("INSERT INTO email SET email = '$email', domain = '$domain', domain_id = '$row', rand = '$rand'");
}
}
fclose($handle);
echo 'success';
exit;
$content = file_get_contents($csvFileName);
print_r($content);exit;
$content = explode("n",$content);

for ($i = 0; $i < count($content); $i++) {
print_r($content);exit;
$randStr = '';
foreach ($content as $value) {
$randStr = randStr(6);
$mail = explode(",",$value);
$companyArr = explode(".",$mail[1]);
$companyName = $companyArr[0];
//echo $companyName;exit;
for ($j = 0; $j < count($emailArr); $j++) {
$email = $emailArr[$j]."@".$mail[1];
echo $email.'<br />';
echo $randStr.'<br />';
echo $companyName.'<br />';
//$sql = mysql_query("INSERT INTO mail_source SET mail = '$email'");
}
}
exit;
}

?>

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved