【问题标题】:Replace values of a csv file替换 csv 文件的值
【发布时间】:2021-08-26 20:36:16
【问题描述】:

我需要使用 PHP 查找和替换 CSV 行的所有值;

我正在尝试这个,但它甚至将标题替换为 0 并且行值并没有像预期的那样翻倍。

public function checkForNumericValues()
    {
        // Read the columns and detect numeric values
        if (($this->handle = fopen($this->csvFile, "r")) !== FALSE) 
        {
            
            $fhandle = fopen($this->csvFile,"r");
            $content = fread($fhandle,filesize($this->csvFile));

            while (($this->data = fgetcsv($this->handle, 1000, ",")) !== FALSE) 
            {
                $this->num = count($this->data);
                
                // Skipping the header
                if($this->row == 1)
                { 
                    $this->row++; 
                    continue; 
                }
                $this->row++;
                

                // Check and replace the numeric values
                for ($j=0; $j < $this->num; $j++) 
                {
                    if(is_numeric($this->data[$j]))
                    {
                        $content = str_replace($this->data[$j], $this->data[$j] * 2, $content);
                    }
                    else
                    {
                        $content = str_replace($this->data[$j], 0, $content);
                    }
                }
                break;
                // print_r($content);
            }
            $fhandle = fopen($this->csvFile,"w");
            fwrite($fhandle,$content);
            fclose($this->handle);
        }
        echo "Numeric and String values been changed in rows of the CSV!";
    }

CSV 是这样的:

【问题讨论】:

  • 文件为什么要打开两次?
  • $this-row的初始值是多少?
  • 为什么要为应该是临时变量的东西使用类属性,比如$this-&gt;num$this-&gt;handle
  • break 语句在文件的第一行之后中断。为什么?

标签: php csv command line


【解决方案1】:

在处理 CSV 中的每个字段时,不应更新整个 $contents,只需更新该字段即可。您的 str_replace() 将替换文件中其他地方的子字符串;例如,如果当前字段包含5,则将文件中的所有5 替换为10,因此125 将变为1210

您可以通过替换$this-&gt;data 数组中的元素来正确执行此操作。完成此操作后,您可以使用implode() 将它们重新加入到字符串中。然后,您可以将所有更新的行保存在一个字符串中,并在最后写回文件。

您可以通过在while 循环之前调用fgets() 来跳过标题行。

public function checkForNumericValues()
{
    // Read the columns and detect numeric values
    if (($this->handle = fopen($this->csvFile, "r")) !== FALSE) 
    {
        $output = "";
        $output .= fgets($this->csvFile); // Copy header line to output

        while (($this->data = fgetcsv($this->handle, 1000, ",")) !== FALSE) 
        {
            $this->num = count($this->data);
                
            // Check and replace the numeric values
            for ($j=0; $j < $this->num; $j++) 
            {
                if(is_numeric($this->data[$j]))
                {
                    $this->data[$j] *= 2;
                }
                else
                {
                    $this->data[$j] = 0;
                }
            }
            $output .= implode(',', $this->data) . "\n";
        }
        fclose($this->handle);
        $fhandle = fopen($this->csvFile,"w");
        fwrite($fhandle,$output);
        fclose($fhandle);
    }
    echo "Numeric and String values been changed in rows of the CSV!";
}

【讨论】:

  • 感谢回复我收到此错误:fgets() 期望参数 1 是资源,给定字符串
  • 我已经添加了这个和它的固定 $stdin = fopen($this-&gt;csvFile, 'r'); $output .= fgets($stdin); 我正在将额外的行添加到全 0 的 csv 中。
  • 只需要弄清楚为什么这个额外的 0 行来自
  • 可能文件末尾有空行?
  • ` // 跳过标题 if($this->row == 1){ $this->row++;继续; } $this->row++;` 添加了这部分现在它可以工作了
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 2017-01-14
  • 2014-03-05
  • 2023-03-17
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多