如何从数组的结果创建.csv文件?

时间:2016-01-28 23:14:13

标签: javascript php jquery html csv

我正在使用PHP类实时检查电子邮件的有效性, PHP脚本工作正常:检查电子邮件并在同一页面中打印结果,为每个经过验证的电子邮件创建<td>元素。

我的问题是我正在尝试验证大约200,000封电子邮件......脚本的结果是一个巨大的列表,导致我的浏览器停止工作。

是否可以使用结果创建.csv文件而不是仅在页面上打印?如果是这样,我该怎么做?

这是我的源代码:

index.php&lt; - 这是显示结果的文件。 emailvalid.php&lt; - 这是PHP类

<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->  
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->  
<!--[if !IE]><!--> <html lang="en"> <!--<![endif]-->  
<head>
    <title>EmailValid</title>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">    
    <link rel="shortcut icon" href="favicon.ico">  
    <link href='http://fonts.googleapis.com/css?family=Lato:300,400,300italic,400italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'> 
    <!-- Global CSS -->
    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
    <!-- Plugins CSS -->    
    <link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="assets/plugins/prism/prism.css">
    <!-- Theme CSS -->  
    <link id="theme-style" rel="stylesheet" href="assets/css/styles.css">
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head> 

<body data-spy="scroll">
    
    <!---//Facebook button code-->
    <div id="fb-root"></div>
    <script>(function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>
    
    <!-- ******PROMO****** -->
    <section id="promo" class="promo section offset-header">
        <div class="container text-center">
            <h2 class="title">Email<span class="highlight">Valid</span></h2>
            <p class="intro">Check the email validity in real-time
			<form method="post">
				<textarea rows="10" cols="150" style="color:black" name="email" placeholder="Insert here your text. The robot will extract all the emails from the text"></textarea>
				</p>
				<div class="btns">
					<input type="submit" value="Check" name="Check" class="btn btn-cta-secondary">
				</div>
			</form>
			<?php include 'assets/emailvalid.php'; ?>
            <ul class="meta list-inline">
            </ul><!--//meta-->
        </div><!--//container-->
    </section><!--//promo-->
     
    <!-- Javascript -->          
    <script type="text/javascript" src="assets/plugins/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="assets/plugins/jquery-migrate-1.2.1.min.js"></script>    
    <script type="text/javascript" src="assets/plugins/jquery.easing.1.3.js"></script>   
    <script type="text/javascript" src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>     
    <script type="text/javascript" src="assets/plugins/jquery-scrollTo/jquery.scrollTo.min.js"></script> 
    <script type="text/javascript" src="assets/plugins/prism/prism.js"></script>    
    <script type="text/javascript" src="assets/js/main.js"></script>       
</body>
</html> 

<?php
$show_details					='yes';   	// choice : 'yes' or 'no'
$disposable_email				='yes';		// choice : 'yes' or 'no'
$check_mx						='yes';		// choice : 'yes' or 'no'

					
if(!empty($_POST['Check'])){
	if(!empty($_POST['email'])){
	
		$emails = get_email($_POST['email']);
		require_once('emailvalidclass.php'); //include emailvalidclass.php file
		$emailChecker = new emailChecker; // Make a new instance
		if($show_details=='yes'){ $emailChecker->supress_output = 0; }else{ $emailChecker->supress_output = 1; }
		if($disposable_email=='yes'){ $emailChecker->filter_dea = 1; }else{ $emailChecker->filter_dea = 0; }
		if($check_mx=='yes'){ $emailChecker->check_mx = 1; }else{ $emailChecker->check_mx = 0; }
		$emailChecker->smtp_test = 0; 

		$emailsubmited=count($emails);	
							
		echo '<br><b>Emails : </b>'.$emailsubmited;
							
							echo '<hr><table cellpadding="10" cellspacing="10" border="1" style="margin-top: 15px;">
									<tr style="font-size:20px;background-color:black;color:white">
										<td>Email</td>
										<td>Passed</td>';
							if($show_details=='yes'){ 
										echo '<td style="text-align:left">Email valid ?</td>';
										if($disposable_email=='yes'){	
											echo '<td>Disposable ?</td>';
										 }  
										if($check_mx=='yes'){
											echo '<td>Domain MX Check ?</td>';
										 }  
							}  
								echo '	</tr>';
							$array_good_emails=array();
							$array_bad_emails=array();
							$array_good_no_duplicate_emails=array();    
							
							foreach((array)$emails as $key=>$e){
							
								if(empty($e)) continue;
							
								$e = $emailChecker->check($e);
								$e = $e['result'];
								// echo '<pre>';
								// print_r($e);
								// echo '</pre>';
								if($e['success']){
									$array_good_emails[]=htmlspecialchars($e['query']);
								}else{
									$array_bad_emails[]=htmlspecialchars($e['query']); 
								}
							?>
						<tr>
							<td><?php echo htmlspecialchars($e['query']); ?></td>
							
							<?php if($e['success']){
										echo '<td style="background-color:green">yes</td>';				
									}else{
										echo '<td style="background-color:red">no</td>';				
									}
							?>
							
							
							<td>
							<?php if(!empty($e['report']['validate_email'])){ 
										echo 'yes';				
									}elseif(!empty($e['errors']['validate_email'])){
										echo 'no';				
									}
							?>
							</td>
<?php if($show_details=='yes'){ ?>
	<?php if($disposable_email=='yes'){ ?>		
						<td>	
								<?php if(!empty($e['report']['filter_dea'])){ 
											echo 'no';
										}elseif(!empty($e['errors']['filter_dea'])){
											echo 'yes';
										}else{
											echo '-';
										}
								?>	
						</td>
	<?php } ?>
	<?php if($check_mx=='yes'){  ?>
								<?php if(!empty($e['report']['check_mx'])){ 
											echo '<td>yes</td>';
										}elseif(!empty($e['errors']['check_mx'])){
											echo '<td>no</td>';
										}else{
											echo '<td>-</td>';
										}
								?>
	<?php } ?>
<?php } ?>
						</tr>

						<?php
							}
							echo '</table><br><br>';
							
						?>
	<div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h4 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">Show Passed/Good Emails</a>
                                        </h4>
                                    </div>
                                    <div id="collapseTwo" class="panel-collapse in" style="height: auto;">
                                        <div class="panel-body" style="color:black">
                                            <?php echo implode("<br>",$array_good_emails); ?>
                                        </div>
                                    </div>
                                </div>				
	<div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h6 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="collapsed">Show Bad Emails</a>
                                        </h6>
                                    </div>
                                    <div id="collapseOne" class="panel-collapse collapse" style="height: 0px;">
                                        <div class="panel-body" style="color:black">
                                            <?php echo implode("<br>",$array_bad_emails); ?>
                                        </div>
                                    </div>
                                </div>
    <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h4 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseFour" class="collapsed">Show Passed/Good Emails (without duplicate emails) </a>
                                        </h4>
                                    </div>
                                    <div id="collapseFour" class="panel-collapse collapse">
                                        <div class="panel-body" style="color:black">
                                            <?php $array_good_no_duplicate_emails=array_unique($array_good_emails); 
												echo implode("<br>",$array_good_no_duplicate_emails); ?>
                                        </div>
                                    </div>
                                </div>				
						
						
		
						<?php

		

	}else{
		echo 'Email field empty. Please, try again';
	}	
}


function get_email($text) {
						## Regex taken from http://bit.ly/Tq2PYP PHP's FILTER_VALIDATE_EMAIL function.
						$email_pattern = "/(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))/i";
						$raw = $text;						
						$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
						$content = str_replace($newlines, "----", html_entity_decode($raw));
						$content2 = preg_replace("/\s/", "-", $content);
						$content2 = preg_replace("/\-+/", "|", $content2);
						$content2 = explode("|", $content2);
						
						$emails = array();
						foreach( $content2 as $line) {
							preg_match("/@/", $line, $match);
							if( count($match) > 0 ) {
								preg_match_all($email_pattern, $line, $email_match);
								if( count($email_match) > 0 ) {
									foreach($email_match[0] as $key=>$email) {
										$emails[] = $email;
									}
								}
							}
						}
						
						return $emails;
					}
?>

我尝试按照@jason的答案给出的说明,现在可以将生成的电子邮件保存在CSV文件中......但是csv文件有重复的信息,我的index.php的标题打印为一列。

这是我的代码:

<?php

$show_details					='no';   	// choice : 'yes' or 'no'
$disposable_email				='no';		// choice : 'yes' or 'no'
$check_mx						='no';		// choice : 'yes' or 'no'

					
if(!empty($_POST['Check'])){
	if(!empty($_POST['email'])){
	
		$emails = get_email($_POST['email']);
		require_once('emailvalidclass.php'); //include emailvalidclass.php file
		$emailChecker = new emailChecker; // Make a new instance
		if($show_details=='yes'){ $emailChecker->supress_output = 0; }else{ $emailChecker->supress_output = 1; }
		if($disposable_email=='yes'){ $emailChecker->filter_dea = 1; }else{ $emailChecker->filter_dea = 0; }
		if($check_mx=='yes'){ $emailChecker->check_mx = 1; }else{ $emailChecker->check_mx = 0; }
		$emailChecker->smtp_test = 0; 

		$emailsubmited=count($emails);
		
	                     
                        
$fileName = 'emails.csv';

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");

$fh = @fopen( 'php://output', 'w' );

$headerDisplayed = false;

$array_good_emails=array();
$array_bad_emails=array();   
							
	foreach((array)$emails as $key=>$e){
    
	// Add a header row if it hasn't been added yet
	if(empty($e)) continue;
							
								$e = $emailChecker->check($e);
								$e = $e['result'];
	
								if($e['success']){
									$array_good_emails[]=htmlspecialchars($e['query']);
								}else{
									$array_bad_emails[]=htmlspecialchars($e['query']); 
								}
								
								
    
    // Put the data into the stream
    fputcsv($fh, $array_good_emails);
 }
// Close the file
fclose($fh);
// Make sure nothing else is sent, our file is done
exit;

							
		
					
                            
                            


							}
							
							
						

		

	}else{
	}	



function get_email($text) {
						## Regex taken from http://bit.ly/Tq2PYP PHP's FILTER_VALIDATE_EMAIL function.
						$email_pattern = "/(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))/i";
						$raw = $text;						
						$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
						$content = str_replace($newlines, "----", html_entity_decode($raw));
						$content2 = preg_replace("/\s/", "-", $content);
						$content2 = preg_replace("/\-+/", "|", $content2);
						$content2 = explode("|", $content2);
						
						$emails = array();
						foreach( $content2 as $line) {
							preg_match("/@/", $line, $match);
							if( count($match) > 0 ) {
								preg_match_all($email_pattern, $line, $email_match);
								if( count($email_match) > 0 ) {
									foreach($email_match[0] as $key=>$email) {
										$emails[] = $email;
									}
								}
							}
						}
						
						return $emails;
					}
?>
						

这是生成csv文件:

<!DOCTYPE html>																											
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->  																											
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->  																											
<!--[if !IE]><!--> <html lang="en"> <!--<![endif]-->  																											
<head>																											
    <title>EmailValid</title>																											
    <!-- Meta -->																											
    <meta charset="utf-8">																											
    <meta http-equiv="X-UA-Compatible" content="IE=edge">																											
    <meta name="viewport" content="width=device-width	 initial-scale=1.0">																										
    <meta name="description" content="">																											
    <meta name="author" content="">    																											
    <link rel="shortcut icon" href="favicon.ico">  																											
    <link href='http://fonts.googleapis.com/css?family=Lato:300	400	300italic	400italic' rel='stylesheet' type='text/css'>																								
    <link href='http://fonts.googleapis.com/css?family=Montserrat:400	700' rel='stylesheet' type='text/css'> 																										
    <!-- Global CSS -->																											
    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">																											
    <!-- Plugins CSS -->    																											
    <link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.css">																											
    <link rel="stylesheet" href="assets/plugins/prism/prism.css">																											
    <!-- Theme CSS -->  																											
    <link id="theme-style" rel="stylesheet" href="assets/css/styles.css">																											
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->																											
    <!--[if lt IE 9]>																											
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>																											
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>																											
    <![endif]-->																											
</head> 																											
																											
<body data-spy="scroll">																											
0																											
    <!-- ******PROMO****** -->																											
    <section id="promo" class="promo section offset-header">																											
        <div class="container text-center">																											
            <h2 class="title">Email<span class="highlight">Valid</span></h2>																											
            <p class="intro">Check ZSBox email list validity in real-time																											
			<form method="post">																											
				<textarea rows="10" cols="150" style="color:black" name="email" placeholder="Insert here your text. The robot will extract all the emails from the text"></textarea>																											
				</p>																											
				<div class="btns">																											
					<input type="submit" value="Check" name="Check" class="btn btn-cta-secondary">																											
				</div>																											
			</form>																											
			aksboneyard@sbcglobal.net																											
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net																										
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com																									
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com																								
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com																							
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com																						
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com																					
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca																				
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com																			
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net																		
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com																	
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com																
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com															
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com														
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com													
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com												
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com	al81g@hotmail.com											
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com	al81g@hotmail.com	alaguvenkat@yahoo.com										
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com	al81g@hotmail.com	alaguvenkat@yahoo.com	al.jones@aig.com									
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com	al81g@hotmail.com	alaguvenkat@yahoo.com	al.jones@aig.com	alagu6@hotmail.com								
aksboneyard@sbcglobal.net	al__franco@sbcglobal.net	aladydot@aol.com	alabama_annie@yahoo.com	alac471@yahoo.com	akulovvpshark@yahoo.com	akw408@yahoo.com	aksingh@bnr.ca	aksnownow@mytalk.com	aktiva@c2i.net	alaa_al_saeed@yahoo.com	aks0355@aol.com	alabamaalex@yahoo.com	al77blackwolf7@email.com	akula_6@yahoo.com	akurt79@yahoo.com	al81g@hotmail.com	alaguvenkat@yahoo.com	al.jones@aig.com	alagu6@hotmail.com	

对此事的任何帮助将不胜感激。 谢谢。

3 个答案:

答案 0 :(得分:0)

要从数组创建CSV行,请使用$csv_line = implode(',',$array_line); 将其吐出到带有CSV扩展名和中提琴的文件中!

此外,如果我看到您正在使用的数组的print_r()示例(例如,如果需要任何循环等),则可以给出更深入的答案。

答案 1 :(得分:0)

你可能已经达到了最大执行力以阻止它超时。

 ini_set('max_execution_time', 300); //300 seconds = 5 minutes

此代码将生成您必须根据需要修改它的csv:

<?php
function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true) {

    if($attachment) {
        // send response headers to the browser
        header( 'Content-Type: text/csv' );
        header( 'Content-Disposition: attachment;filename='.$filename);
        $fp = fopen('php://output', 'w');
    } else {
        $fp = fopen($filename, 'w');
    }

    $result = mysql_query($query, $db_conn) or die( mysql_error( $db_conn ) );

    if($headers) {
        // output header row (if at least one row exists)
        $row = mysql_fetch_assoc($result);
        if($row) {
            fputcsv($fp, array_keys($row));
            // reset pointer back to beginning
            mysql_data_seek($result, 0);
        }
    }

    while($row = mysql_fetch_assoc($result)) {
        fputcsv($fp, $row);
    }

    fclose($fp);
}

// Using the function
$sql = "SELECT * FROM table";
// $db_conn should be a valid db handle

// output as an attachment
query_to_csv($db_conn, $sql, "test.csv", true);

// output to file system
query_to_csv($db_conn, $sql, "test.csv", false);
?>

答案 2 :(得分:0)

这就是你需要的:

$fileName = 'emails.csv';

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");

$fh = @fopen( 'php://output', 'w' );

$headerDisplayed = false;

foreach ( $your_emails_array as $data ) {
    // Add a header row if it hasn't been added yet
    if ( !$headerDisplayed ) {
        // Use the keys from $data as the titles
        fputcsv($fh, array_keys($data));
        $headerDisplayed = true;
    }

    // Put the data into the stream
    fputcsv($fh, $data);
 }
// Close the file
fclose($fh);
// Make sure nothing else is sent, our file is done
exit;