【问题标题】:Saving array as CSV file using PHP使用 PHP 将数组保存为 CSV 文件
【发布时间】:2018-05-02 11:49:00
【问题描述】:

这段代码通过获取两个网站的所有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之前

【问题讨论】:

    标签: php arrays string csv string-comparison


    【解决方案1】:

    确实会输出 HTML,因为这是您的代码所做的第一件事。

    如您所知,PHP 允许在单个文件中混合 HTML 和代码,通过使用 &lt;?php?&gt; 标记来表示 PHP 代码的开始和结束。

    这些标记之外的所有内容都被视为输出,并以与您使用print()echo() 语句完全相同的方式发送到浏览器。

    您的代码以一段 HTML 开头,之前没有任何内容。这与使用包含此 HTML 的 print() 语句启动程序完全相同。

    如果您不想在所有情况下都输出 HTML,那么您需要在它之前添加一些代码来告诉它何时以及是否输出它。

    【讨论】:

      【解决方案2】:

      如果已经有输出,则不能使用header()

      将表单 html 移动到第一个 else 语句中。

      另外,请确保启用错误报告,因为 php 会让您清楚这一点。

      【讨论】:

        猜你喜欢
        • 2017-03-15
        • 1970-01-01
        • 2019-04-15
        • 2014-03-20
        • 2016-08-25
        • 2021-06-25
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        相关资源
        最近更新 更多