使用 PHP 将数组保存为 CSV 文件
Saving array as CSV file using PHP
此代码通过获取两个网站的所有 href
属性来分析这两个网站的内容。然后,它从每个数组中找到与 href 值最匹配的那些,并将它们保存到 CSV 文件中。问题是,当我打开文件时,它还会带回我的应用程序的 HTML 数据。
作为练习,我只能使用 HTML 和 PHP
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
website:
<input type="text" name="website1">
<br>
website:
<input type="text" name="website2">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit']))
{
// form has been submitted
$url1 = $_POST['website1'];
$url2 = $_POST['website2'];
findAndCompare($url1, $url2);
}
else
{
}
function findAndCompare($url1, $url2)
{
libxml_use_internal_errors(true);
// Create a DOM parser object
$dom1 = new DOMDocument();
$dom2 = new DOMDocument();
$dom1->loadHTMLFile($url1);
$dom2->loadHTMLFile($url2);
$arr1 = array();
$arr2 = array();
$arr3 = array();
// Iterate over all the <a> tags
foreach($dom1->getElementsByTagName('a') as $link)
{
// insert the <a href> in arr1
array_push($arr1, $link->getAttribute('href'));
}
// Iterate over all the <a> tags
foreach($dom2->getElementsByTagName('a') as $link)
{
// insert the <a href> in arr2
array_push($arr2, $link->getAttribute('href'));
}
for ($i = 0; $i < count($arr1); $i++)
{
$max_elem = $arr2[0];
$max = 0;
for ($j = 0; $j < count($arr2); $j++)
{
similar_text($arr1[$i], $arr2[$j], $perc);
if ($perc > $max)
{
$max = $perc;
$max_elem = $arr2[$j];
}
}
$tmp = array($arr1[$i],$max_elem,$max);
array_push($arr3,$tmp);
}
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
$temp_memory = fopen('php://memory', 'w');
// loop through the array
foreach($input_array as $line)
{
// use the default csv handler
fputcsv($temp_memory, $line, $delimiter);
}
fseek($temp_memory, 0);
// modify the header to be CSV format
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
// output the file to be downloaded
fpassthru($temp_memory);
}
convert_to_csv($arr3, 'export.csv', ',');
}
?>
更新
我解决了把 ob_clean();
放在 header
之前
确实会输出 HTML,因为这是您的代码所做的第一件事。
如您所知,PHP 允许在单个文件中混合使用 HTML 和代码,方法是使用 <?php
和 ?>
标记来表示开始和 PHP 代码的结尾。
这些标记之外的所有内容都被视为输出,并以与您使用 print()
或 echo()
语句完全相同的方式发送到浏览器。
您的代码以 HTML 块开始,之前没有任何内容。这与您使用包含此 HTML.
的 print()
语句启动程序完全相同
如果你不希望HTML在所有情况下都输出,那么你需要在它之前添加一些代码来告诉它何时以及是否输出它。
如果已经有输出,则不能使用header()
。
将表格 html 移动到第一个 else 语句中。
此外,请确保启用错误报告,因为 php 会向您说明这一点。
此代码通过获取两个网站的所有 href
属性来分析这两个网站的内容。然后,它从每个数组中找到与 href 值最匹配的那些,并将它们保存到 CSV 文件中。问题是,当我打开文件时,它还会带回我的应用程序的 HTML 数据。
作为练习,我只能使用 HTML 和 PHP
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
website:
<input type="text" name="website1">
<br>
website:
<input type="text" name="website2">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit']))
{
// form has been submitted
$url1 = $_POST['website1'];
$url2 = $_POST['website2'];
findAndCompare($url1, $url2);
}
else
{
}
function findAndCompare($url1, $url2)
{
libxml_use_internal_errors(true);
// Create a DOM parser object
$dom1 = new DOMDocument();
$dom2 = new DOMDocument();
$dom1->loadHTMLFile($url1);
$dom2->loadHTMLFile($url2);
$arr1 = array();
$arr2 = array();
$arr3 = array();
// Iterate over all the <a> tags
foreach($dom1->getElementsByTagName('a') as $link)
{
// insert the <a href> in arr1
array_push($arr1, $link->getAttribute('href'));
}
// Iterate over all the <a> tags
foreach($dom2->getElementsByTagName('a') as $link)
{
// insert the <a href> in arr2
array_push($arr2, $link->getAttribute('href'));
}
for ($i = 0; $i < count($arr1); $i++)
{
$max_elem = $arr2[0];
$max = 0;
for ($j = 0; $j < count($arr2); $j++)
{
similar_text($arr1[$i], $arr2[$j], $perc);
if ($perc > $max)
{
$max = $perc;
$max_elem = $arr2[$j];
}
}
$tmp = array($arr1[$i],$max_elem,$max);
array_push($arr3,$tmp);
}
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
$temp_memory = fopen('php://memory', 'w');
// loop through the array
foreach($input_array as $line)
{
// use the default csv handler
fputcsv($temp_memory, $line, $delimiter);
}
fseek($temp_memory, 0);
// modify the header to be CSV format
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
// output the file to be downloaded
fpassthru($temp_memory);
}
convert_to_csv($arr3, 'export.csv', ',');
}
?>
更新
我解决了把 ob_clean();
放在 header
确实会输出 HTML,因为这是您的代码所做的第一件事。
如您所知,PHP 允许在单个文件中混合使用 HTML 和代码,方法是使用 <?php
和 ?>
标记来表示开始和 PHP 代码的结尾。
这些标记之外的所有内容都被视为输出,并以与您使用 print()
或 echo()
语句完全相同的方式发送到浏览器。
您的代码以 HTML 块开始,之前没有任何内容。这与您使用包含此 HTML.
的print()
语句启动程序完全相同
如果你不希望HTML在所有情况下都输出,那么你需要在它之前添加一些代码来告诉它何时以及是否输出它。
如果已经有输出,则不能使用header()
。
将表格 html 移动到第一个 else 语句中。
此外,请确保启用错误报告,因为 php 会向您说明这一点。