Php 替换csv文件的值
我需要使用PHP查找并替换CSV中所有行的值 我正在尝试这样做,但它甚至将标题替换为0,行值也不会像它想象的那样加倍Php 替换csv文件的值,php,csv,command,line,Php,Csv,Command,Line,我需要使用PHP查找并替换CSV中所有行的值 我正在尝试这样做,但它甚至将标题替换为0,行值也不会像它想象的那样加倍 public function checkForNumericValues() { // Read the columns and detect numeric values if (($this->handle = fopen($this->csvFile, "r")) !== FALSE)
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!";
}
公共函数checkfornumericfalues()
{
//读取列并检测数值
if($this->handle=fopen($this->csvFile,“r”))!==FALSE)
{
$fhandle=fopen($this->csvFile,“r”);
$content=fread($fhandle,filesize($this->csvFile));
而($this->data=fgetcsv($this->handle,1000,“,”)!==FALSE)
{
$this->num=count($this->data);
//跳过标题
如果($this->row==1)
{
$this->row++;
继续;
}
$this->row++;
//检查并替换数值
对于($j=0;$j<$this->num;$j++)
{
如果(是数值($this->data[$j]))
{
$content=str_replace($this->data[$j],$this->data[$j]*2,$content);
}
其他的
{
$content=str_replace($this->data[$j],0,$content);
}
}
打破
//打印(内容);
}
$fhandle=fopen($this->csvFile,“w”);
fwrite($fhandle,$content);
fclose($this->handle);
}
echo“CSV行中的数值和字符串值已更改!”;
}
CSV是这样的:
处理CSV中的每个字段时,不应更新整个
$contents
,只需更新该字段即可。str\u replace()
将替换文件中其他位置的子字符串;例如,如果当前字段包含5
,您将用10
替换文件中的所有5,因此125
将变为1210
您可以通过替换$this->data
数组中的元素来正确执行此操作。完成此操作后,您可以使用内爆()
将它们连接回一个字符串。然后,您可以将所有更新的行保存在一个字符串中,并在最后将其写回文件
通过在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!";
}
公共函数checkfornumericfalues()
{
//读取列并检测数值
if($this->handle=fopen($this->csvFile,“r”))!==FALSE)
{
$output=“”;
$output.=fgets($this->csvFile);//将标题行复制到输出
而($this->data=fgetcsv($this->handle,1000,“,”)!==FALSE)
{
$this->num=count($this->data);
//检查并替换数值
对于($j=0;$j<$this->num;$j++)
{
如果(是数值($this->data[$j]))
{
$this->data[$j]*=2;
}
其他的
{
$this->data[$j]=0;
}
}
$output.=内爆(“,”,$this->data)。“\n”;
}
fclose($this->handle);
$fhandle=fopen($this->csvFile,“w”);
fwrite($fhandle,$output);
fclose($fhandle);
}
echo“CSV行中的数值和字符串值已更改!”;
}
为什么要打开文件两次?$this row
的初始值是多少?为什么要将类属性用于应该是临时变量的对象,例如$this->num
和$this->handle
?语句在文件的第一行之后中断。为什么?感谢您的回复,我收到了这个错误:fgets()希望参数1是资源,字符串givenI已经添加了这个参数及其固定的$stdin=fopen($this->csvFile,'r')$输出=fgets($stdin)代码>我正在将所有0的额外行添加到csv中。只需想一想为什么这些额外的0行来自文件末尾可能有一个空行?`//如果($this->row==1){$this->row++;continue;}$this->row++;`添加了此部分,现在可以使用了